遍历XML结构

时间:2014-09-23 00:51:11

标签: javascript xml rhino

我有以下XML文档:

<order> 
        <book ISBN="0942407296"> 
            <title>Baking Extravagant Pastries with Kumquats</title> 
            <author> 
                <lastName>Contino</lastName> 
                <firstName>Chuck</firstName> 
            </author> 
            <pageCount>238</pageCount> 
        </book> 
        <book ISBN="0865436401"> 
            <title>Emu Care and Breeding</title> 
            <editor> 
                <lastName>Case</lastName> 
                <firstName>Justin</firstName> 
            </editor> 
            <pageCount>115</pageCount> 
        </book> 
    </order>

为了获得标题,我使用:

myXML.book[0].title[0]

如何使用myXML.book...获取ISBN(&#34; 0942407296&#34;)?

2 个答案:

答案 0 :(得分:0)

var final_r= new java.util.ArrayList(); 
stringBuilder = new java.lang.StringBuilder(); 
var xml_ISBN= new XML(stringBuilder); 
var final_rez = new Array(); 
//open the XML for reading 
var file = new java.io.FileReader (XML_location)); 
while ((line=file.readLine()) !=null) { 
//how many ISBN we have in the XML 
if (line.contains ("ISBN")) counter=counter+1; } 
} 
for (var i=0;i<counter;i++) { 
final_rez[i]=xml_ISBN.book[i]; var convert_str=(xml_ESI.book[i]).toString(); 
} 

答案 1 :(得分:0)

// get XML 
var xml = xhr.responseXML;

// get tag
var tags = xml.getElementsByTagName("tagnamehere");
for (var i = 0; i < users.length; i++) {   
var tag = tags[i].firstChild.nodeValue;
var tr = document.createElement("tr");
var td = document.createElement("td");
var textNode = document.createTextNode(tag);
td.appendChild(textNode);        
tr.appendChild(td);        
document.getElementById("tbody").appendChild(tr);
}