473,378 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Processing an XML document in Javascript

69
Hi,

I have a Javascript function that creates a Ajax request to retrieve data from a database using a PHP script. The data returned by the PHP script is an XML Document that I access through transport.responseXML. The structure of the XML document is as follows:
Expand|Select|Wrap|Line Numbers
  1. <job>
  2. <contactTelephone>028 ---------</contactTelephone>
  3. <contactName>Karen -------</contactName>
  4. <contactFax>028 -----------</contactFax>
  5. <contactEmail>karen@---------</contactEmail>
  6. <contactWebsite>www.---------</contactWebsite>
  7. </job>
I use var xmlOptions = xmlDoc.getElementsByTagName('job'); to get job and then xmlOptions[0].getElementsByTagName('contactTelephone')[0].firstChild.data but can I use a for loop to process the XML document tag by tag without hardcoding it? Using a variable in getElementsByTagName?

Thanks,

Sean
Jul 21 '07 #1
7 4167
epots9
1,351 Expert 1GB
Hi,

I have a Javascript function that creates a Ajax request to retrieve data from a database using a PHP script. The data returned by the PHP script is an XML Document that I access through transport.responseXML. The structure of the XML document is as follows:

<job>
<contactTelephone>028 ---------</contactTelephone>
<contactName>Karen -------</contactName>
<contactFax>028 -----------</contactFax>
<contactEmail>karen@---------</contactEmail>
<contactWebsite>www.---------</contactWebsite>
</job>

I use var xmlOptions = xmlDoc.getElementsByTagName('job'); to get job and then xmlOptions[0].getElementsByTagName('contactTelephone')[0].firstChild.data but can I use a for loop to process the XML document tag by tag without hardcoding it? Using a variable in getElementsByTagName?

Thanks,

Sean
i got mine working with for loops try this:

Expand|Select|Wrap|Line Numbers
  1. var xmlData = xmlDoc.documentElement;
  2.  
the u would have to do:

Expand|Select|Wrap|Line Numbers
  1. alert(xmlData.childNodes[0].childNodes[0].nodeValue);
  2.  
the second snipplet u will have to change....depending on your xml structure.

good luck
Jul 22 '07 #2
pbmods
5,821 Expert 4TB
Heya, Sebarry. Welcome to TSDN!

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Jul 22 '07 #3
Sebarry
69
i got mine working with for loops try this:

Expand|Select|Wrap|Line Numbers
  1. var xmlData = xmlDoc.documentElement;
  2.  
the u would have to do:

Expand|Select|Wrap|Line Numbers
  1. alert(xmlData.childNodes[0].childNodes[0].nodeValue);
  2.  
the second snipplet u will have to change....depending on your xml structure.

good luck
Hi, Can you post your code in full. I've tried what you suggested but I can't get it to work. Sorry :(.
Jul 22 '07 #4
epots9
1,351 Expert 1GB
Hi, Can you post your code in full. I've tried what you suggested but I can't get it to work. Sorry :(.
i read your pm, looking at your xml file, test this out:

Expand|Select|Wrap|Line Numbers
  1. alert(xmlData.childNodes[0].nodeValue);
  2.  
each childNode is like going one step down...i think we went too far before.

try it and post back.

good luck
Jul 22 '07 #5
Sebarry
69
Hi, me again.

Sorry I seriously do not know what I'm doing wrong. Here's the Javascript code.

Expand|Select|Wrap|Line Numbers
  1.  
  2. new Ajax.Request('/iwo/libs/GetJobInfo.php', {
  3.         method: 'get',
  4.           parameters: 'jobIdentifier=' + jobID,
  5.           onSuccess: function(transport){
  6.  
  7.             var xmlDoc = transport.responseXML;
  8.  
  9.             var xmlData = xmlDoc.documentElement;
  10.             alert(xmlData);
  11.  
  12.             try
  13.             {
  14.                 alert(xmlData.childNodes[0].nodeValue);
  15.             }
  16.             catch( err )
  17.             {    
  18.                 alert(err);
  19.             }
  20.  
  21.  
Followed by the XML document.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <job>
  5.  
  6.   <jobContactTelephoneTxt>028 9035 2066</jobContactTelephoneTxt>
  7.  
  8.   <jobContactNameTxt>Karen Topping</jobContactNameTxt>
  9.  
  10.   <jobContactFaxTxt>028 9035 2161    </jobContactFaxTxt>
  11.  
  12.   <jobContactEmailTxt>jobs@biosearch.co.uk</jobContactEmailTxt>
  13.  
  14.   <jobContactWebsiteTxt>biosearch.co.uk</jobContactWebsiteTxt>
  15.  
  16.   <jobLocationOpt>Ireland</jobLocationOpt>
  17.  
  18.   <jobStatusOpt></jobStatusOpt>
  19.  
  20.   <jobTitleTxt>Microbiologist</jobTitleTxt>
  21.  
  22.   <jobCompanyTxt>Bio-Search (N.I.) Ltd.</jobCompanyTxt>
  23.  
  24.   <jobReferenceTxt>IWO/ED/17138</jobReferenceTxt>
  25.  
  26.   <jobSalaryRdo>Negotiable</jobSalaryRdo>
  27.  
  28.   <notesFCKEditor>Responsible for: operation of a dedicated algae and cryptosporidium laboratory; maintenance
  29.  
  30.  of UKAS accreditation; reporting of results; conducting internal and external quality control.  Experience
  31.  
  32.  of cryptosporidium analysis essential.</notesFCKEditor>
  33.  
  34.   <jobStartOpt>2007-06-15</jobStartOpt>
  35.  
  36.   <jobCloseOpt>2007-06-29</jobCloseOpt>
  37.  
  38.   <jobLogoImg></jobLogoImg>
  39.  
  40.   <jobCategoryOpt>Scientific</jobCategoryOpt>
  41.  
  42. </job>
  43.  
  44.  
The PHP does the following at the end to complete the XML document.

Expand|Select|Wrap|Line Numbers
  1.  
  2. $dom->formatOutput = true;
  3. print $dom->saveXML();
  4.  
  5.  
Please help :(

Sean
Jul 26 '07 #6
pbmods
5,821 Expert 4TB
Heya, Sean.

What do you want your code to do? Give an example.
What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
Jul 26 '07 #7
Sebarry
69
Hiya,

Thanks for replying. I should change my profile so I know when someone has replied. In answer to your question of what I'm wanting to achieve. I want to write a generic piece of code that will process a XML document given the name of the document root i.e. job. The code should retrieve the node name and node value.

What I have so far is:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function getJobInfo(jobID)
  3. {
  4.       new Ajax.Request('/iwo/libs/GetJobInfo.php', {
  5.           method: 'get',
  6.           parameters: 'jobIdentifier=' + jobID,
  7.           onSuccess: function(transport){
  8.  
  9.     var xmlDoc = transport.responseXML;
  10.     var xmlOptions = xmlDoc.getElementsByTagName('job');
  11.  
  12.     alert("Length is: " + xmlOptions.length );
  13.  
  14.     for( i = 0; i < xmlOptions.length; i++ ) 
  15.     {
  16.           alert( "Node name is: " + xmlOptions.item(i).tagName );
  17.           alert( "Node value is: " + xmlOptions.item(i).nodeValue );
  18.     }
  19.  
  20.  
Expand|Select|Wrap|Line Numbers
  1. /iwo/libs/GetJobInfo.php
retrieves data from a database and creates and populates the XML document that I want to parse. The XML document will only ever contain the details of one job. As follows.

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <job>
  3.   <jobContactTelephoneTxt>028 9035 2066</jobContactTelephoneTxt>
  4.   <jobContactNameTxt>Karen Topping</jobContactNameTxt>
  5.   <jobContactFaxTxt>028 9035 2161</jobContactFaxTxt>
  6.   <jobContactEmailTxt>jobs@biosearch.co.uk</jobContactEmailTxt>
  7.   <jobContactWebsiteTxt>biosearch.co.uk</jobContactWebsiteTxt>
  8.   <jobLocationOpt>Ireland</jobLocationOpt>
  9.   <jobStatusOpt></jobStatusOpt>
  10.   <jobTitleTxt>Microbiologist</jobTitleTxt>
  11.   <jobCompanyTxt>Bio-Search (N.I.) Ltd.</jobCompanyTxt>
  12.   <jobReferenceTxt>IWO/ED/17138</jobReferenceTxt>
  13.   <jobSalaryRdo>Negotiable</jobSalaryRdo>
  14.   <notesFCKEditor>Responsible for: operation of a dedicated algae and   
  15.        cryptosporidium laboratory; maintenance of UKAS accreditation; reporting
  16.        of results; conducting internal and external quality control.  Experience
  17.        of cryptosporidium analysis essential.</notesFCKEditor>
  18.   <jobStartOpt>2007-06-15</jobStartOpt>
  19.   <jobCloseOpt>2007-06-29</jobCloseOpt>
  20.   <jobLogoImg></jobLogoImg>
  21.   <jobCategoryOpt>Scientific</jobCategoryOpt>
  22. </job>
  23.  
However
Expand|Select|Wrap|Line Numbers
  1. alert("Length is: " + xmlOptions.length );
reports a length of 1 and
Expand|Select|Wrap|Line Numbers
  1. alert( "Node name is: " + xmlOptions.item(i).tagName );
  2. and alert( "Node value is: " + xmlOptions.item(i).nodeValue );
outputs Job and null respectively. Its the nodes under job that I want not job.

I've tried this that works but it requires me to know how many nodes there are and it's not very elegant.

Expand|Select|Wrap|Line Numbers
  1. alert(transport.responseXML.documentElement.firstChild.text);
  2. alert(transport.responseXML.documentElement.firstChild.nextSibling.nodeName);
  3. alert(transport.responseXML.documentElement.firstChild.nextSibling.text);
  4. aler(transport.responseXML.documentElement.firstChild.nextSibling.nextSibling.nodeName);
  5. alert(transport.responseXML.documentElement.firstChild.nextSibling.nextSibling.text);
  6.  
Hope you can help,

Sean
Heya, Sean.

What do you want your code to do? Give an example.
What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
Jul 29 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sylvie Stone | last post by:
Hi group - I have an html form for that uses username and password to login to a specific area of the website. The "area" the user wants to go to is based on a pull down menu. Becasue the...
4
by: Fabian | last post by:
I have the following lopp inside one of my scripts document.getElementById('text').innerHTML = ""; for (i = 0; i < txtdef.length; i++) { if (xx == txtdef) { if (yy == txtdef) { if (level ==...
4
by: Bob | last post by:
Below is sample code that illustrates what I'm trying to do. For sake of brevity I didn't include the properties of buildBtn that determine what data to request. The problem is I never see...
2
by: Edward | last post by:
The below code builds 2 tables 4 rows by 4 cols. All cells have checkboxes. When checked, the checkboxes in the first column automatically check the remainder of the check boxes in the same row. ...
1
by: Brian Henry | last post by:
I haven't worked with java script much, I know how to do this server side by constant post backs to the server on a webform, but it seems like i should be able to do this client side also... ...
3
by: thomas.porschberg | last post by:
Hi, I want to read records from a database and export it in an arbitrary format. My idea was to feed a class with a String array fetched from the database and let this class fire SAX events as...
1
by: medvegonok | last post by:
In the following code I am trying to print out the value of document.body.onclick from javascript: <BODY onclick="alert('ok')"> ... <script type="text/javascript">...
1
by: bibhukdas | last post by:
Hi All, Just to elaborate on the issue. The requirement is to handle this in javascript 1. User clicks on a button 2. Change the cursor style (We are using document.body.style.cursor="wait") 3....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.