5

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?

6
  • (for starters - have a look here and the resources it points to w3fools.com). Secondly your javascript doesn't seem to match your xml? you are looking for a node code fruits1 but your xml has 'design' with the title. Also this seems to be a duplicate - stackoverflow.com/questions/8599218/… Commented Aug 23, 2013 at 13:23
  • [email protected] updated my answer..ya your correct my xml can`t match this var name = xmlDoc.getElementsByTagName("design") line..it results to be [object HTMLCollection]..how to call this attribute? Commented Aug 23, 2013 at 13:30
  • Since you are using jquery anyway, it may become easier with this - api.jquery.com/jQuery.parseXML Commented Aug 23, 2013 at 13:37
  • Hi @techfoobar..Thank you for response my question..I want it in javascript becz entire file i working in javascript.Hope i have one query when i used in jquery means it will affect my javascript ?Can you advise me.. Commented Aug 23, 2013 at 13:39
  • Oh.. i thought i saw the jquery tag in the question earlier. Maybe i thought it wrong! :-) Commented Aug 23, 2013 at 13:40

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@user2711066 : sorry i dint get you? is it working for you now?
your code worked perfectly...I want to place it in my option value..i will also worked and show correct value.
@user2711066 : yes i just pointed out how to pick up the attribute value title from the nodelist you got which your code was not doing.
@mithunsatheesh..this code will more helpful for me...my select box will show correctly..Thank you for make me happy..and i need to clarify some thing.I want to read xml file using javascript do you know any kind of tutorial..
@user2711066 : you can check this SO answer which has the same code you are using explained. Also reading Parsing_and_serializing_XML and how-to-parse-a-XMl-file-with-a-jquery will give more clarity.
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.