0

I've been looking around for a while and have found things close to but not what I'm looking for.

I'm try to take this XML:

<?xml version="1.0"?>
<document name="New Document">
    <url>http://nsc-component.webs.com/Office/Editor/new-doc.html?docname=New+Document&titletype=Title&fontsize=9&fontface=Arial&spacing=1.0&text=&wordcount3=0</url>
</document>

I know it's not valid, it's just an example

And make it into a JavaScript array that writes it like so:

<a href="URL-TAG-VALUE">NAME ATTRIBUTE HERE</a>

For each <document> tag

Can anyone help?

2 Answers 2

1

You should probably parse the XML string before working with it :

if (window.DOMParser) {
    var parser = new DOMParser(),
    xml = parser.parseFromString(your_xml_string_here,"text/xml");
} else { // Internet Explorer
    var xml = new ActiveXObject("Microsoft.XMLDOM");

    xml.async = false;
    xml.loadXML(your_xml_string_here); 
}

Then you can access the DOM of the XML like you would HTML:

var arr = [],
    documents = xml.getElementsByTagName('document');

for (var i = 0; i < documents.length; i++) {
    var anchor = document.createElement('a'),
        url    = documents[i].getElementsByTagName('url')[0];


    anchor.href = url.innerText || url.textContent;
    anchor.innerHTML = documents[i].getAttribute('name');

    arr.push(anchor);
}

Your XML as it's shown in the question is not valid, and will however fail the parsing.

FIDDLE

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

4 Comments

That's awesome! But would it work for every <document> tag that was in the XML?
@brandonjordan - Yes it would, see this FIDDLE
Wow that's awesome! Thanks so much! I know I've asked for a lot so far but is there anyway I can put that into a <div>, add an <img/> before it, some &nbsp;'s? If not can I at least add a class so I can style it?
@brandonjordan - you can add anything you want, and build whatever HTML suits your needs. Here's another FIDDLE
1

Although your XML isn't valid, and jQuery is not a solution for everything, it does have a nice function called jQuery.parseXML(). Here's a possible solution:

//assign the XML string to a variable
var xmlstring = '<?xml version="1.0"?><document name="New Document"><url>http://stackoverflow.com</url></document>';

//use jQuery's XML parsing function and assign it to a variable
var xmldoc = $.parseXML(xmlstring);

//allow jQuery to handle the elements like HTML
var xml = $(xmldoc);

//find <url> and get its contents
var url = xml.find('url').text();

//find <document> and get its name attribute
var doc = xml.find('document').attr('name');

In this example, url will return http://stackoverflow.com and doc will return New Document.

3 Comments

Using jQuery will avoid some cross-browser issues with parsing XML.
Ok, thanks for the suggestion, I'll try that once I have some time to edit my code
any chance you could make an example on jsfiddle? et cetera? pardon. I'm not too fluent in jQuery.

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.