2017-05-30 27 views
0

我发现了很多这个问题的工作示例,但没有人在我的案例中工作。我将从服务器获得以下响应,并需要在“消息”标签之间获取值。使用javascript获取xml标记之间的值

<?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```[email protected]@@DjwPIUpA=p</Message> 
<Status>true</Status> 
<Code>200</Code> 
</BaseResponse> 

将不胜感激,如果任何人都可以请帮我整理一下。

PS: - 对于参考我尝试了用http://jsfiddle.net/RPbSE/,但没有成功,根据你的榜样

回答

1

所有你需要做的使用DOM解析器

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```[email protected]@@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); 

被得到消息标记的内部HTML这里有一个工作捣鼓你:https://jsfiddle.net/zeq5g47t/

0

你可以尝试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>[email protected]@@DjwPIUpA=p</Message> 
<Status>true</Status> 
<Code>200</Code> 
</BaseResponse>`,"application/xml"); 
doc.querySelector("Message").innerHTML