Jump to content

Accesing Specific XML elements


StupidRalph

Recommended Posts

All right I have a javascript that parses my .xml files and writes the contents to individual elements in my page via the innerHTML method. That is okay for one page but what if I want to have all my pages in one xml file? Ex. I have a page called home.xml that has all the content for the home page. Another xml file for an About us page in about_us.xml and etc. What if I didn't want to have them in seperate files but in one xml file called site.xml? How would I tell my javascript which to node to use. I tried like changing firstChild to secondChild and so forth. I gave each page an id attribute but didn't know how to access it. Any help will be appreciated. parse_xml.js

var xmlDocfunction loadXML(){//load xml file// code for IEif (window.ActiveXObject)  {  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");  xmlDoc.async=false;  xmlDoc.load("xml/site.xml");  getmessage()  }// code for Mozilla, etc.else if (document.implementation &&document.implementation.createDocument)  {  xmlDoc= document.implementation.createDocument("","",null);  xmlDoc.load("xml/site.xml");  xmlDoc.onload=getmessage  }else  {  alert("Your browser cannot handle this script");  }}function getmessage(){document.getElementById("contentheader").innerHTML=xmlDoc.getElementsByTagName("contentheader")[0].firstChild.nodeValuedocument.getElementById("content").innerHTML=xmlDoc.getElementsByTagName("content")[0].firstChild.nodeValue}

site.xml

<?xml version="1.0" encoding="iso-8859-1"?><page id="1"><name>Home</name><title>Company Name</title><contentheader>• Our Company</contentheader><content><span style="width: 460px; padding-left: 20px; ">Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</span></content></page><page id="2"><name>About Us</name><title>Company Name2</title><contentheader>• About Us</contentheader><content><span style="width: 460px; padding-left: 20px; ">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span></content></page>....etc.

Suppose I wanted my Javascript to write the elements of page 2 and page 2 only.

Link to comment
Share on other sites

Okay class here is what to do. You see that last part part in my javascript?This part

document.getElementById("content").innerHTML=xmlDoc.getElementsByTagName("content")[0].firstChild.nodeValue

Well you see the 0 inside of the braces []. Yeah the second line of code just before it says firstChild-- that is called an index. Increase the index to which ever element you want to call. The reason it didn't work the first time I tried this was b/c I had errors on my XML page. Its really BASIC javascript that I was overthinking. It is also a good idea if you set your site up like me to set your page id equal to the index in the XML file. Tune in next week and I will show you how to declare a variable. DOH! I should've been past this.

Link to comment
Share on other sites

  • 1 month later...

So does this code work with IE & Mozilla and if so do u mind if i use the javascript coz i tried writing my own and its jst not working...

Okay class here is what to do.  You see that last part part in my javascript?This part
document.getElementById("content").innerHTML=xmlDoc.getElementsByTagName("content")[0].firstChild.nodeValue

Well you see the 0 inside of the braces []. Yeah the second line of code just before it says firstChild-- that is called an index.  Increase the index to which ever element you want to call.  The reason it didn't work the first time I tried this was b/c I had errors on my XML page.  Its really BASIC javascript that I was overthinking.  It is also a good idea if you set your site up like me to set your page id equal to the index in the XML file.  Tune in next week and I will show you how to declare a variable. DOH! I should've been past this.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...