简体   繁体   中英

How to call xml (title) tags using Javascript

I have one xml file which main root starts with:

<design title="standard Cards 3.5 x 2" >
      <previews></previews>
      <previews1></previews1>
      <previews2></previews2>
   </design>

I want to read that xml files and get title tags and assign in option value.

I also got this design root tags by using this w3school method .i want to get design --> title and stored into my select box:

Here my code:

var selectHTML = "";
selectHTML += "<select name='something' id='media' onchange='select();'>";
for (i = 0; i < total.length; i = i + 1) {
    if (total[i] != '') {
        xmlDoc = loadXMLDoc($loc);
        var fruits = xmlDoc.documentElement.nodeName;
        if (fruits) {
            alert(fruits);
            var name = xmlDoc.getElementsByTagName("design")
            alert(name);
        }
        selectHTML += "<option value='" + total[i] + "'>" + name + "</option>";
    }
}
selectHTML += "</select>";

Here xmlDoc=loadXMLDoc($loc); is calling method of xml files.I refer this from w3school tutorial

I just alerting name it resulting undefined. How to solve this?

I want to place name value into my select box option value. How to read xml files using javascript and place into option value?

try by

var name = xmlDoc.getElementsByTagName("design");
var value = name[0].getAttribute('title')
alert(value);

also in the w3school example, the loadXMLDoc is a function which is defined manually. It uses XMLHttpRequest() and ActiveXObject("Microsoft.XMLHTTP") According to your browser type. So if you dint include loadXMLDoc definition, then there will be errors.

I'll take a punt at this.

  • Assuming that your $loc is just the filename...

from what I can tell loadXMLDoc() will load an xml document from the same context as the document that executes it (in this case the html page)

If the page executing that JavaScript is in a different directory to the XMl file it will fail to load.

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