将XML转换为普通的旧JavaScript对象?
给定此示例XML:将XML转换为普通的旧JavaScript对象?,javascript,jquery,xml,object,Javascript,Jquery,Xml,Object,给定此示例XML: <patient> <name> <given>Bob</given> <family>Dole</family> </name> </patient> 上下快速移动 救济金 我想创建一个对象,patient,并且能够执行类似于alert(patient.name.given)的操作,并获得一个显示“Bob”的弹出窗口。我的实际
<patient>
<name>
<given>Bob</given>
<family>Dole</family>
</name>
</patient>
上下快速移动
救济金
我想创建一个对象,patient
,并且能够执行类似于alert(patient.name.given)
的操作,并获得一个显示“Bob”的弹出窗口。我的实际数据要比这复杂得多,所以我还需要考虑属性和数组
如何做到这一点
我目前正在使用parseXML()
,但我不想键入alert($xml.find(“patient”).find(“name”).find(“given”).text)
下面是一个如何将xml解析(解组)为JavaScript的示例:
将XML解析为JS
我不明白,什么是
PO
?它看起来像是由java实用程序生成的。需要吗?我没有模式或XSD,从XSD自动生成映射需要Java,但是如果没有XSD或Java,也可以手动生成映射。看,我不明白为什么我必须生成或手动创建映射。我真的只想要一个镜像XML的对象。映射仅仅定义了镜像XML的含义。如果XML文件是
,那么这就是我镜像的内容。为什么我必须创建一个映射,上面写着文档{person{name{}}}
??
// Include or require PO.js so that PO variable is available
// For instance, in node.js:
var PO = require('./mappings/PO').PO;
// First we construct a Jsonix context - a factory for unmarshaller (parser)
// and marshaller (serializer)
var context = new Jsonix.Context([PO]);
// Then we create a unmarshaller
var unmarshaller = context.createUnmarshaller();
// Unmarshal an object from the XML retrieved from the URL
unmarshaller.unmarshalURL('po.xml',
// This callback function will be provided
// with the result of the unmarshalling
function (unmarshalled) {
// Alice Smith
console.log(unmarshalled.value.shipTo.name);
// Baby Monitor
console.log(unmarshalled.value.items.item[1].productName);
});