How to call xml tags (title) using Javascript

I have one xml file from which the main root starts:

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

I want to read these xml files and get the header tags and assign them in the parameter value.

I also got these design tags with this w3school method. I want to get designtitle and saved in my selection box:

Here is 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); calls the xml file method. I refer to this from the w3school tutorial

I just warn name , resulting in undefined. How to solve this?

I want to put the name value in the value of the block selection option. How to read xml files using javascript and put in parameter value?

+4
source share
2 answers

try

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

also in the w3school example, loadXMLDoc is a function that is defined manually. It uses XMLHttpRequest() and ActiveXObject("Microsoft.XMLHTTP") depending on the type of your browser. Therefore, if you include the definition of loadXMLDoc , then there will be errors.

+2
source

I will take a punt on this.

  • Assuming your $ loc is just a file name ...

from what I can say, loadXMLDoc () will load the XML document from the same context as the document that executes it (in this case the html page)

If the page that runs this JavaScript is in a different directory in the XMl file, it will not load.

+1
source

Source: https://habr.com/ru/post/1498604/


All Articles