任何XML中的特定Javascript对象

Specific Javascript Object from any XML

本文关键字:Javascript 对象 XML 任何      更新时间:2023-09-26

在我的服务器上有数百个xml文件。其中一个文件可能看起来像这样:

<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader>
    <fileDesc>
        <titleStmt>
            <title>Diary of Robert Graves 1935-39 and ancillary material</title>
            <author>Robert Graves</author>
            <editor>...</editor>
        </titleStmt>
        <publicationStmt>
            <publisher>...</publisher>
            <pubPlace>...</pubPlace>
            <availability status="unknown">
                <p>...</p>
            </availability>
            <date>...</date>
        </publicationStmt>
        <sourceDesc>
            <p>...</p>
        </sourceDesc>
    </fileDesc>
    <profileDesc>
        <particDesc>
            <listPerson>
                <person xml:id="AH">
                    <p>Alan Hodge. Oxford history graduate. Became close friends with Laura Riding &amp; Robert Graves. First husband of Beryl Graves.</p>
                </person>
                <person xml:id="BP">
                    <p>Beryl Pritchard. Daughter of Harry and Amy Pritchard, Robert Graves's second wife. Formerly married to Alan Hodge. Robert and Beryl had four children: William, Lucia, Juan and Tomas.</p>
                </person>
                <person xml:id="DR">
                    <p>David Reeves. Brother of James Reeves.</p>
                </person>
            </listPerson>
        </particDesc>
    </profileDesc>
</teiHeader>
<facsimile>
    <surface xml:id="graves1938-10-10-1">
        <graphic url="graves1938-10-10.jpg"/>
    </surface>
</facsimile>
<text>
    <front>
        <div type="abstract">
            <head>OCTOBER 1938</head>
            <p>The rains set in, and Graves works in his bedroom with the fire going. ...</p>
        </div>
    <!-- abstracts for other months -->
    </front>
    <body>
        <div type="diaryentry" n="1938-10-10">
            <head>Oct 10 <del>Tuesday.</del><add>Monday</add></head>
            <p>Ghost, completing <abbr>ch</abbr> IX</p>
            <p>Dictionary with <rs ref="#AH">Alan</rs>.</p>
            <p>A lot of time goes to making charcoal for 'Marthe',  <rs ref="#BP">Beryl</rs>'s now using this <foreign>fugon</foreign> <note>charcoal-burner</note> for warming her attic.</p>
            <p>Went to Montauban with <rs ref="#DR">David</rs> – first visit for about 10 days – <del>got</del> <add>ordered</add> small wood for  Dorothy's cresset.</p>
            <p>Now almost always win at Cambeluk: we are playing a correspondence game with Harry.</p>
            <p>Nono broke Laura's particular coffee cup, <figure><figDesc>sketch of cup</figDesc></figure> and she her blue glass bottle given by Karl.</p>
        </div>
        <!-- other entries -->
    </body>
</text>

为了更清楚地表述它:xml文件唯一共享的是它们的文件扩展名,但没有结构xml模式。


对于驻留在我的服务器上的xml文件中的每一个,我都想生成一个Javascript对象,对于上面的示例,它看起来如下:

{
root: {
    text: "TEI",
    children: [{text: "teiHeader", 
                children:[{text: "fileDesc", 
                           children:[{text:"titleStmt", 
                                      children:[{text:"title"},{text:"author"},{text:"editor"}]},
                                     {text:"publicationStmt", 
                                      children:[{text:"publisher"},{text:"pubPlace"},{text:"availability", 
                                                                                      children:[{text:"p"}]},{text:"date"}]},
                                     {text:"sourceDesc", 
                                      children:[{text:"p"}]}]},
                          {text: "profileDesc", 
                           children:[{text:"particDesc",
                                      children:[{text:"listPerson", 
                                                 children:[{text:"person"},{text:"person"},{text:"person"}]}]}]}]},
               {text:"fascimile",  
                children:[{text:"surface", 
                           children:[{text:"graphic"}]}]},
               {text:"text", 
                children:[{text:"front", 
                           children:[{text:"div", 
                                      children:[{text:"head"},{text:"p"}]}]},
                          {text:"body",
                           children:[{text:"div", 
                                      children:[{text:"head", 
                                                 children:[{text:"del"},{text:"add"}]},{text:"p", 
                                                                                        children:[{text:"abbr"}]},{text:"p", 
                                                                                                                   children:[{text:"rs"}]},{text:"p", 
                                                                                                                                            children:[{text:"rs"},{text:"foreign"},{text:"note"}]},{text:"p", 
                                                                                                                                                                                                    children:[{text:"rs"},{text:"del"},{text:"add"}]},{text:"p"},{text:"p",
                                                                                                                                                                                                                                                                  children:[{text:"figure",
                                                                                                                                                                                                                                                                             children:[{text:"figDesc"}]}]}]}]}]}]
}
}

换句话说:xml树结构通过在子属性中嵌套内部标记和在文本属性中嵌套标记名来映射到Javascript对象。


我手动键入了上面的示例。我不知道,但尽管原理看起来很简单,但我认为它相当复杂。

我想到使用纯客户端javascript XML Parser解析XML,您可以在这里找到它。

也许在API之上的中加入Sax-Parser,我可以使用萨克斯事件(如SAXDocumentHandler.startElement (name, atts)SAXDocumentHandler.endElement (name))来做一些事情。

但是,即使使用这个解析器,由于XML的可变结构,我也很难实现一些东西。我想不出任何聪明的方法来迭代它

你能帮我构造Javascript对象吗

你试过了吗https://code.google.com/p/x2js/?

这似乎是你需要的:

此库将XML提供给JSON(JavaScript对象),反之亦然javascript转换函数。图书馆很小,没有需要任何其他附加库。