Link to home
Start Free TrialLog in
Avatar of Charlietn
Charlietn

asked on

How do I parse XML file in JAVA Script

I have an xml file created from a join.  The XML file contains duplicates from the PARENT record.

Parent Name Parent Address Child Data
Parent Name Parent Address Child Data
Parent Name Parent Address Child Data

I can pull the child data but the duplicates throw me.

I need help with the code
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Useful if you had posted the actual xml

Try

var items = xmlDoc.getElementsByTagName("whateverTag")
for (var i=0,i<items.length;i<n;i++) ....
Avatar of Charlietn
Charlietn

ASKER

Thanks, Sorry for not posting the code.  The XML file is attached as well as the code I use to make a table out of the child table.  What I am having trouble with is the parent record.  

Thanks for your patience.  I am trying to learn by doing.  

Charlie
<?xml version="1.0" encoding="windows-1252" standalone="yes" ?> 
- <QUERY queryName="_QSQL560">
  <Contract_no>123</Contract_no> 
  <Fname>Brandon</Fname> 
  <Lname>Thomas</Lname> 
  <Add1>123 ANY STR</Add1> 
  <City>Anytown</City> 
  <St>NC</St> 
  <Zip>29988</Zip> 
  <Tel>8888888888</Tel> 
  <Tel_2>9999999999</Tel_2> 
  <Club>The Club</Club> 
  <Email>bthomas@bthomas.com</Email> 
  <Contr_dt /> 
  <Due_dt /> 
  <LastCall_dt /> 
  <Contract_no_1>123</Contract_no_1> 
  <CallDate>08/01/2011</CallDate> 
  <Rep>CDR</Rep> 
  <Disposition>N/A</Disposition> 
  <Note>Tried to call this person many times</Note> 
  <Contract_no>123</Contract_no> 
  <Fname>Brandon</Fname> 
  <Lname>Thomas</Lname> 
  <Add1>123 ANY STR</Add1> 
  <City>Anytown</City> 
  <St>NC</St> 
  <Zip>29988</Zip> 
  <Tel>8888888888</Tel> 
  <Tel_2>9999999999</Tel_2> 
  <Club>The Club</Club> 
  <Email>bthomas@bthomas.com</Email> 
  <Contr_dt /> 
  <Due_dt /> 
  <LastCall_dt /> 
  <Contract_no_1>123</Contract_no_1> 
  <CallDate>08/02/2011</CallDate> 
  <Rep>LWOODY</Rep> 
  <Disposition>N/A</Disposition> 
  <Note>Customer is upset</Note> 
  <Contract_no>123</Contract_no> 
  <Fname>Brandon</Fname> 
  <Lname>Thomas</Lname> 
  <Add1>123 ANY STR</Add1> 
  <City>Anytown</City> 
  <St>NC</St> 
  <Zip>29988</Zip> 
  <Tel>8888888888</Tel> 
  <Tel_2>9999999999</Tel_2> 
  <Club>The Club</Club> 
  <Email>bthomas@bthomas.com</Email> 
  <Contr_dt /> 
  <Due_dt /> 
  <LastCall_dt /> 
  <Contract_no_1>123</Contract_no_1> 
  <CallDate>08/10/2011</CallDate> 
  <Rep>TDAVIS</Rep> 
  <Disposition>L/M</Disposition> 
  <Note>Customer said to call when husband is home tonight</Note> 
  </QUERY>


Java to make disps tag into table

var xmlEl = response.getElementsByTagName("DISP");
    var table = document.createElement("table");
    table.border = "0";
    table.width="95%";
  table.align="CENTER";
    var tbody = document.createElement("tbody");
    // Append the body to the table
    table.appendChild(tbody);
    var row = document.createElement("tr");
    // Append the row to the body
    tbody.appendChild(row);
    for (colHead = 0; colHead < xmlEl[0].childNodes.length; colHead++) {
        if (xmlEl[0].childNodes[colHead].nodeType != 1) {
            continue;
        }
        var tableHead = document.createElement("th");
        var colName = document.createTextNode(xmlEl[0].childNodes[colHead].nodeName);
        tableHead.align="left"
  tableHead.style.fontFamily = "VERDANA";
  tableHead.style.fontSize = "12px";
  tableHead.style.backgroundColor = "#E0F2F5";
  
  tableHead.appendChild(colName);
        row.appendChild(tableHead);
    }
    tbody.appendChild(row);
    // Create table row
    for (i = 0; i < xmlEl.length; i++) {
        var row = document.createElement("tr");
        // Create the row/td elements
        for (j = 0; j < xmlEl[i].childNodes.length; j++) {
            // Skip it if the type is not 1
            if (xmlEl[i].childNodes[j].nodeType != 1) {
                continue;
            }
            // Insert the actual text/data from the XML document.
            var td = document.createElement("td");
            var xmlData = document.createTextNode(xmlEl[i].childNodes[j].firstChild.nodeValue);
            if (i % 2) {
                td.style.backgroundColor = "#E0F2F5";
            }
            td.style.fontFamily = "Verdana";    
            td.style.fontSize = "12px";
            td.appendChild(xmlData);
            row.appendChild(td);
        }
        tbody.appendChild(row);
    }
    document.getElementById("xmldata").appendChild(table);
--

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Had I posted a better question it would have been a perfect answer
What was the perfect answer?

I'd love to help you but I cannot find what you meant by parent
There wasn't one<G>   I was making it more difficult than it was.  Sorry for the poorly worded question.

Thanks again for your help.  It is appreciated.
Good. You had me worried there...