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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read xml data and Load to innerHTML

Status
Not open for further replies.

Lekoya

Programmer
Aug 4, 2006
6
ZA
Hi There,
I have been trying to read xml data and loading inside a div tag depending on what the user clicks;

I have the following xml example :
-----
<rootnode>

<company ID="85" name="Cyber Makers">
Web Developers
</company>
<company ID="86" name="Lekoya Makers">
Dummy Data for Lekoya
</company>


</rootnode>
---

What I want achive to do is Load Information between company tags "<company></company>" inside a div and display a pop up div.

Here is my code :
======
var xmlDoc
function loadXML()
{
//load xml file
// code for IE
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("../temp_files/test.xml");
getmessage()
}
// code for Mozilla, etc.
else if (document.implementation &&
document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("../tmp_files/test.xml");
xmlDoc.onload=getmessage
}
else
{
alert('Your browser cannot handle this script');
}
}

function getmessage()
{
document.getElementById("content").innerHTML=xmlDoc.childNodes[1].firstChild.nodeValue
}
======

With the above code i get the follwing error :
xmlDoc.childNodes[1] has no properties.

I am using mozilla firefox for browsing.

Please Help
 
try changing your getmessage() function to this:

Code:
document.getElementById("content").innerHTML=xmlDoc.getElementsByTagName("company")[1].innerHTML;

does that suit your needs?



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
If by index [1] you mean to retrieve the 2nd company in the source, it is done like this cross-browser.
[tt]
function getmessage() {
var icompany=-1;
var itargetidx=1; //zero-based; given
var cnode=xmlDoc.[blue]documentElement.[/blue]childNodes;
for (var i=0;i<cnode.length;i++) {
if (cnode.nodeType==1 && cnode.nodeName=="company") {
if (++icompany==itargetidx) {
document.getElementById("content").innerHTML=cnode.childNodes[0].nodeValue;
break;
}
}
}
}
[/tt]
That's already some sort of minimal you've to do. There contains some "noises" in the innerHTML not visible in the html rendering. To eliminate those "noises", replace the corresponding line by taking out the whitespaces.
[tt]
document.getElementById("content").innerHTML=cnode.childNodes[0].nodeValue.replace(/^\s+(.*?)\s+$/,"$1");
[/tt]
 
Thanx tsuji for help, but i havent got it working yet.

Now I am getting the following error on this line:
var cnode=xmlDoc.documentElement.childNodes;

Here is the error :
xmlDoc.documentElement has no properties:

Any other idea as to why am i getting this error:

Thanks in advance.
 
>xmlDoc.documentElement has no properties:
If you get that error, I would think either xmlDoc is not established or that the loading encounter parsing error or can't find the source file. I suggest you test in ie simply inserting at the beginning of getmessage().
[tt] if (navigator.userAgent.indexOf("MSIE")!=-1) {
alert(xmlDoc.xml);
}[/tt]
You should be able to see the xml source as such in ie. Anything going wrong, you would probably see a blank. Test it out?
 
Hi tsuji,
I have done exactly as you advised from the above post.

The alert comes up blank. So how can i solve this problem.
One other thing is that i can view the xml source if type the path to the xml file.

Any other suggesstion.

Here is my xml :

=======
Code:
<?xml version="1.0" ?> 
- <data>
   <company ID="89" name="Pandora Bra Studio">Our personal             attention & the perfect bra are guaranteed
   </company> 
   <company ID="217" name="MillionAds">The world's most advanced online business directory.</company> 
   <company ID="64" name="Cash Converters">Trading Hours: Mon - Fri 9:00 - 18:00 Saturday 8:30 - 17:00 Sunday 9:00 - 14:00
  </company> 
  <company ID="183" name="CARISMA CAR HIRE">Carisma Car Hire has been operating only in Cape Town for three years, and the bakkie hire has been operating for 10 years - under the name Supervans Bakkie and Truck hire.</company> 
  <company ID="95" name="ABSOLUTELY WATER">Wide range of reverse osmosis purifiers</company> 
  </data>
=======
 
Your xml element's text should be technically called CDATA, the that the first company contains "&" which should be escaped to &amp;. The other elements suffer none of this problem. Hence, a quich fix is to change the first element to this.
[tt]
<company ID="89" name="Pandora Bra Studio">Our personal attention &[red]amp;[/red] the perfect bra are guaranteed
</company>
[/tt]
The general solution often clumsy reflecting the idea is this.
[tt]
<company ID="89" name="Pandora Bra Studio">[blue]<![CDATA[[/blue]Our personal attention & the perfect bra are guaranteed[blue]]]>[/blue]
</company>
[/tt]
It is applicable to the rest and that's the idea.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top