javascript的XML解析

由于需要熟悉mirth,其中JavaScript部分是非常重要的,特别是其XML解析部分,不知道

 

var getPatientByMayIDResponseXML = new XML(getPatientByMayIDResponse);

.*

实例HL7中的XML类

var temp1 = getPatientByMayIDResponseXML.*.toString();
<soap:Header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"/>
<soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
	<ns2:getPatientByMapIDResponse xmlns:ns2="http://services.cxf.shengjy.vico.com/">
		<return>
			<objectList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:patient">
				<id>321</id>
				<name>bbb</name>
				<sex>f</sex>
				<mapToID>11</mapToID>		
			</objectList>
		</return>
	</ns2:getPatientByMapIDResponse>
</soap:Body>
<span style="font-family:Arial;BACKGROUND-COLOR: #ffffff"></span>

.*表示该XML文本本身,返回的temp1会显示该节点下的XML文本,如果是最初的XML文本则会去掉申明

 

var temp2 = getPatientByMayIDResponseXML..*toString();
<soap:Header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"/>
<soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
	<ns2:getPatientByMapIDResponse xmlns:ns2="http://services.cxf.shengjy.vico.com/">
		<return>
			<objectList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:patient">
				<id>321</id>
				<name>bbb</name>
				<sex>f</sex>
				<mapToID>11</mapToID>
			</objectList>
		</return>
	</ns2:getPatientByMapIDResponse>
</soap:Body>
<ns2:getPatientByMapIDResponse xmlns:ns2="http://services.cxf.shengjy.vico.com/">
	<return>
		<objectList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:patient">
			<id>321</id>
			<name>bbb</name>
			<sex>f</sex>
			<mapToID>11</mapToID>
		</objectList>
	</return>
</ns2:getPatientByMapIDResponse>
<return>
	<objectList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:patient">
		<id>321</id>
		<name>bbb</name>
		<sex>f</sex>
		<mapToID>11</mapToID>
	</objectList>
</return>
<objectList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:patient">
	<id>321</id>
	<name>bbb</name>
	<sex>f</sex>
	<mapToID>11</mapToID>
</objectList>
<id>321</id>
321
<name>bbb</name>
bbb
<sex>f</sex>
f
<mapToID>11</mapToID>
11

..*将会一级一级的解析其所有的子节点并返回

 

var aa = getPatientByMayIDResponseXML..*::['name'].toString();

::['']的写法与..*连用

bbb

 

var aa = getPatientByMayIDResponseXML..*['name'].toString();
bbb

返回值相同,都正确解析了最底层的节点
那么两者有什么不同之处呢?

var aa = getPatientByMayIDResponseXML.*['name'].toString();
var aa = getPatientByMayIDResponseXML.*::['name'].toString();

两个都返回空值,所以没搞清楚.*有什么用处,写和没写效果没什么区别的样子

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用JavaScript内置的XML解析器`DOMParser`来解析XML,然后将解析结果转换为JSON格式。以下是一个示例代码: ```javascript function xmlToJson(xml) { // Create the DOM parser const parser = new DOMParser(); // Parse the XML string const xmlDoc = parser.parseFromString(xml, "text/xml"); // Get the root element of the XML document const root = xmlDoc.documentElement; // Convert the XML document to a JSON object return convertToJson(root); } function convertToJson(node) { const obj = {}; if (node.nodeType === 1) { // element node if (node.attributes.length > 0) { obj["@attributes"] = {}; for (let i = 0; i < node.attributes.length; i++) { const attr = node.attributes.item(i); obj["@attributes"][attr.nodeName] = attr.nodeValue; } } } else if (node.nodeType === 3) { // text node obj = node.nodeValue; } if (node.hasChildNodes()) { for (let i = 0; i < node.childNodes.length; i++) { const childNode = node.childNodes.item(i); const nodeName = childNode.nodeName; if (typeof(obj[nodeName]) === "undefined") { obj[nodeName] = convertToJson(childNode); } else { if (typeof(obj[nodeName].push) === "undefined") { const oldObj = obj[nodeName]; obj[nodeName] = []; obj[nodeName].push(oldObj); } obj[nodeName].push(convertToJson(childNode)); } } } return obj; } ``` 使用方法: ```javascript const xmlString = "<bookstore><book category='cooking'><title lang='en'>Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book><book category='children'><title lang='en'>Harry Potter</title><author>J.K. Rowling</author><year>2005</year><price>29.99</price></book></bookstore>"; const json = xmlToJson(xmlString); console.log(json); ``` 输出结果: ```json { "bookstore": { "@attributes": {}, "book": [ { "@attributes": { "category": "cooking" }, "title": { "@attributes": { "lang": "en" }, "#text": "Everyday Italian" }, "author": { "#text": "Giada De Laurentiis" }, "year": { "#text": "2005" }, "price": { "#text": "30.00" } }, { "@attributes": { "category": "children" }, "title": { "@attributes": { "lang": "en" }, "#text": "Harry Potter" }, "author": { "#text": "J.K. Rowling" }, "year": { "#text": "2005" }, "price": { "#text": "29.99" } } ] } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值