简体   繁体   中英

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

Based on your example all you have to do is get the inner html of the message tag using a dom parser

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/

You could try the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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