gpt4 book ai didi

javascript - 来自任何 XML 的特定 Javascript 对象

转载 作者:行者123 更新时间:2023-11-28 07:14:23 24 4
gpt4 key购买 nike

我的服务器上有数百个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-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 树结构映射到 Javascript 对象。

<小时/>

上面的例子是我手动输入的。我不知道,虽然原理看起来很简单,但我认为它相当复杂。

我想到使用纯客户端 javascript Xml-Parser解析 XML,您可以找到 here .

也许通过将 Sax-Parser 合并到上述 API 中,我可以使用 Sax-Events 做一些事情,例如 SAXDocumentHandler.startElement (名称,属性)SAXDocumentHandler.endElement(名称)

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

你能帮我构建 Javascript 对象吗?

最佳答案

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

看来这正是您所需要的:

This library provides XML to JSON (JavaScript Objects) and vice versa javascript conversion functions. The library is very small and doesn't require any other additional libraries.

关于javascript - 来自任何 XML 的特定 Javascript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30998867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com