任何XML中的特定Javascript对象
Specific Javascript Object from any XML
在我的服务器上有数百个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 & 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转换函数。图书馆很小,没有需要任何其他附加库。
相关文章:
- Chrome开发工具(如何知道我在调用哪个javascript对象)
- 循环遍历以数组为值的Javascript对象
- 从ajax请求中获取javascript对象
- 如何从对象的原型方法访问JavaScript对象属性
- 将XML转换为普通的旧JavaScript对象
- 通过引用传递JavaScript对象
- javascript对象操作:根据指定条件选择属性
- Javascript对象类在单击时打开窗口进行颜色选择,并在更改时替换对象背景颜色
- 如何在异步函数中使用javascript对象
- 临时Javascript对象
- 如何在ASP中为用户控件添加Javascript对象网
- 使用数组向下搜索Javascript对象
- Rails将JavaScript对象存储到Model的有效方式
- JavaScript对象不是从原型链继承的
- 如何创建具有默认值的JavaScript对象字段?(AngularJS模型相关)
- SetInterval在javascript对象中表现怪异
- Javascript 对象和 this 关键字
- 如何在不知道关键字的情况下访问javascript对象值
- 在 JavaScript 对象中设置要使用的运算符的属性
- 如何搜索JavaScript对象并更改值