在我的服务器上有数百个 xml-files 。其中一个文件可能如下所示:
<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 & 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-files 唯一共享的是它们的文件扩展名,但没有结构化的xml模式。
对于驻留在我的服务器上的每个 xml-files ,我想生成一个 Javascript-Object ,这对于上面的例子来说如下:
{
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-tree-structure通过嵌套在text-properties中的children-properties和tag-names中的内部标记映射到Javascript-object。
我手动输入上面的例子。我不知道,但是虽然原理看起来很简单,但我觉得它很复杂。
我想到使用纯客户端javascript Xml-Parser 解析,您可以找到here。
也许将 Sax-Parser 合并到上面的上面我可以使用像<{1}}这样的 Sax-Events 来做一些事情SAXDocumentHandler.startElement (name, atts)
。
但是即使使用这个解析器,由于我的XML的变量结构,我正在努力实现某些东西。我无法想出任何巧妙的方法来迭代它。
你能帮我构建Javascript-Objects吗?
答案 0 :(得分:0)
您是否尝试过https://code.google.com/p/x2js/?
似乎这就是你所需要的:
此库为JSON(JavaScript对象)提供XML,反之亦然 javascript转换功能。图书馆很小,没有 需要任何其他额外的库。