[英]how to get element from XML using javascript
我必須將項目形式的XML更改為JavaScript(至少其中某些部分)。 所以我有這樣的建設:
$xml = simplexml_load_file("http://...);
$profile = $xml->profile;
$user_id = $profile->user_id;
現在我想將其翻譯成JavaScript,所以我使用了:
var xmlHttp_subscribe = new XMLHttpRequest();
xmlHttp_subscribe.onreadystatechange=postCall;
xmlHttp_subscribe.open("GET","http://...",true);
xmlHttp_subscribe.send();
現在函數postCall()
function postCall(){
var t = document.getElementsByName("MY_API").value;
alert('t'+t);
var p = document.getElementsByName("profile").value;
alert('p'+p);
var h = document.getElementsByName("user_id").value;
//...//
}
XML在我的http://下是這樣的:
<MY_API>
<profile>
<user_id>the_user_id</user_id>
</profile>
</MY_API>
我想做的是將此'the_user_id'部分作為純文本字符串。 有誰知道如何做到這一點? 我的方向是否正確?
感謝您的任何幫助。
沒有函數“ getElementsByName”。 您需要的是getElementsByTagName。
正如Pineda所建議的那樣,正確的函數名稱是getElementsByTagName
,此外正確的屬性名稱不是“ value”而是nodeValue
,因此您應該使用
function postCall(){
var h = document.getElementsByTagName("user_id").nodeValue;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.