Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ParseXML 1

Status
Not open for further replies.

borntorun

MIS
Oct 16, 2003
82
GB
Hi,

Could you help me know how to parse the following xml in java script so i can get the text?

<test>
<country>brazil</country>
<town>rio</town>
<person>
<name>ronaldo</name>
<name>socrates</name>
<name>zico</name>
<name>pele</name>
</person>
</test>

Need to extract values.

This is in IE so can use msft xml.

Need to put text into three variables.

One for each tag.

Thank you all.



 
You do not explain where do you get the file. Let me suppose it is already read as textstream into a variable and work from there.
[tt]
var sxml="<test>\r\n" +
"<country>brazil</country>\r\n" +
"\t<town>rio</town>\r\n" +
"\t<person>\r\n" +
"\t\t<name>ronaldo</name>\r\n" +
"\t\t<name>socrates</name>\r\n" +
"\t\t<name>zico</name>\r\n" +
"\t\t<name>pele</name>\r\n" +
"\t</person>\r\n" +
"</test>";
var oparser=new ActiveXObject("Msxml2.DOMDocument");
oparser.loadXML (sxml);
var scountry=oparser.selectSingleNode("/test/country").text;
var stown=oparser.selectSingleNode("/test/town").text;
var operson=oparser.selectSingleNode("/test/person");
var aoname=operson.childNodes

//this shows you what you get and how to retrieve names
alert(socuntry + "\n" + stown + "\n" + aoname.length + "\n" + aoname[0].text + "\n"+aoname[1].text + "\n"+aoname[2].text + "\n" + aoname[3].text);
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top