简体   繁体   English

使用javascript获取xml标签之间的值

[英]get value between xml tags using javascript

I have found a lot of working example for this problem but no one is working in my case. 我已经找到了许多解决此问题的有效示例,但在我的情况下没有人在工作。 I will be having following response from server and need to get value between "Message" tags. 我将收到服务器的以下响应,并且需要在“消息”标签之间获取值。

<?xml version="1.0" encoding="utf-8"?>
<BaseResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 xmlns="http://tempuri.org/">
<Message>uYnxLHnBJPtBp9K8GNg```F4v5YPP4HIgOxN@@@DjwPIUpA=p</Message>
<Status>true</Status>
<Code>200</Code>
</BaseResponse>

will be grateful if anyone can please help me to sort it out. 如果有人可以帮助我将其整理出来,将不胜感激。

PS:- For reference I tried it using http://jsfiddle.net/RPbSE/ but no success PS:-作为参考,我尝试使用http://jsfiddle.net/RPbSE/进行了尝试,但未成功

Based on your example all you have to do is get the inner html of the message tag using a dom parser 根据您的示例,您要做的就是使用dom解析器获取message标签的内部html。

var text, parser, xmlDoc;

text = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<BaseResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://tempuri.org/\">" +
"<Message>uYnxLHnBJPtBp9K8GNg```F4v5YPP4HIgOxN@@@DjwPIUpA=p</Message>" +
"<Status>true</Status>" + 
"<Code>200</Code>" +
"</BaseResponse>";

parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");


alert(xmlDoc.getElementsByTagName("Message")[0].innerHTML );

Here's a working fiddle for you: https://jsfiddle.net/zeq5g47t/ 这是为您工作的小提琴: https : //jsfiddle.net/zeq5g47t/

You could try the DOMParser api 您可以尝试DOMParser API

var parser = new DOMParser();
var doc = parser.parseFromString(`<?xml version="1.0" encoding="utf-8"?>
<BaseResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns="http://tempuri.org/">
<Message>uYnxLHnBJPtBp9K8GNgF4v5YPP4HIgOxN@@@DjwPIUpA=p</Message>
<Status>true</Status>
<Code>200</Code>
</BaseResponse>`,"application/xml");
doc.querySelector("Message").innerHTML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM