Aaron de Windt
Aaron de Windt

Reputation: 17708

How do I create and read an xml file with jquery

I know there is alot of information to do this in the internet, but I got very confused.

I want to create an xml file and send it with Ajax to the server. I will need to reseve and xml file back.

This is an example of the XML file I will need to create.

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <item>dkfjgn</item>
    <item>sdfxvv</item>
    <item>htyjhj</item>
    <item>vnmvbn</item>
    <item>qqqgdb</item>
<data>

As you see it is a very basic xml file. the values of the items will be contained in an array.

This is the xml file it will be send back.

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <dkfjgn>
        <title>A title</title>
        <discription>A discription</discription>       
    </dkfjgn>

    <sdfxvv>
        <title>A title</title>
        <discription>A discription</discription>
    </sdfxvv>

    <htyjhj>
        <title>A title</title>
        <discription>A discription</discription>       
    </htyjhj>

    <vnmvbn>
        <title>A title</title>
        <discription>A discription</discription>       
    </vnmvbn>

    <qqqgdb>
        <title>A title</title>
        <discription>A discription</discription>       
    </qqqgdb>
<data>

After reseving the xml file I want to use an "for each" on the array containing the values and look for the values and do something.

Example

for ( var i in values )    
{
    var title = "";
    var discription = "";

    /*********************************************************************************\
     * Each value can be obtained here by using values[i].                           *
     * If there is an other way to do this with jquery please.                       *
     * Here I will need to parse the xml file and the values in the variables above. *
    \*********************************************************************************/  
}

Upvotes: 0

Views: 2005

Answers (1)

fviktor
fviktor

Reputation: 2958

  • Reading XML

http://think2loud.com/reading-xml-with-jquery/

  • Writing XML

It seems there's no direct way other than building your XML with string operations. According to an answer to this question "there's a cross-browser JavaScript way to get XML from an XML DOM node". The answer refers to this another question.

I hope it helps.

Upvotes: 1

Related Questions