Read data from XML file

100 views
Skip to first unread message

Malintha

unread,
Aug 29, 2012, 8:47:01 AM8/29/12
to fir...@googlegroups.com
Hi,


I want to read a XML file which has data for my extension. I want to read that file inside the extension and put data to an array. I tried to create DOM object parser and read from that object in my extension .But is not working properly, Here is  part of my code


var systemPrincipal = Components.classes["@mozilla.org/systemprincipal;1"]
                      .createInstance(Components.interfaces.nsIPrincipal);

var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
             .createInstance(Components.interfaces.nsIDOMParser);



var doc = parser.parseFromString("<?xml version=1.0 encoding=ISO-8859-1?><TestSuite xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexsi:noNamespaceSchemaLocation=file://C:/seleniumLab/vtaf/vtaf/xsd/TestSuite.xsd ><TestCase name=logintoChorus><LoadPage>LoginPage<LoadPage/>    </TestCase></TestSuite>", "application/xml");


alert(doc); //this gives me the xmldoc object

alert(doc.getElementByTagName("LoadPage"));  //this gives me nothing


I am new to DOM object also ? Can you give me any advice


Thanks

Sebastian Zartner

unread,
Aug 29, 2012, 8:55:27 AM8/29/12
to fir...@googlegroups.com
alert(doc.getElementByTagName("LoadPage"));  //this gives me nothing

You should use alert(doc.getElementsByTagName("LoadPage").item(0)) instead.
getElementsByTagName() returns an array-like NodeList.

Sebastian

Malintha

unread,
Aug 29, 2012, 9:09:10 AM8/29/12
to fir...@googlegroups.com
var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
             .createInstance(Components.interfaces.nsIDOMParser);

var doc = parser.parseFromString("<test><TestSuite>swg</TestSuite></test>", "application/xml");


alert(doc.getElementByTagName("TestSuite").item(0));

//I thing i should have the alert "swg"    :(   but get nothing

-----------------------------------------------------------------------------------------------------------------------------------------------

now this is my code. But still I get nothing. Is there any problem in the method when I m  creating a DOM parser. Shall have to initialize it after creating.

Sebastian Zartner

unread,
Aug 29, 2012, 9:34:55 AM8/29/12
to fir...@googlegroups.com
Could it be that you just forgot the "s" in getElementsByTagName()?

Sebastian

Sebastian Zartner

unread,
Aug 29, 2012, 9:37:52 AM8/29/12
to fir...@googlegroups.com
Ah, and you need to add .textContent at the end to get the tag's content.

Malintha

unread,
Aug 29, 2012, 12:15:52 PM8/29/12
to fir...@googlegroups.com
Thanks Sebastian, It is working now  

Sebastian Zartner

unread,
Aug 31, 2012, 2:03:08 AM8/31/12
to fir...@googlegroups.com
Thanks Sebastian, It is working now
Great. If you have other issues like this I suggest you put your XML into a separate file, load it inside the browser and try out your DOM functions using the Firebug Command Line.

Sebastian
Reply all
Reply to author
Forward
0 new messages