#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Help XML and XML DOM


    Hello! I'm new to the community, so it's nice to meet all of you. My name is Michael and I'm an aspiring software engineer with roots in web development. I'm happy to have finally found a place where I can receive feedback and assistance on any code I may have a problem with or require feedback for.

    Okay, so, here's the issue:

    I have recently started experimenting with XML and XML DOM to try to add a little bit of dynamics to the process of exchanging and altering data on my sites. Even though that to my knowledge my syntax is straight and correct, I am still not receiving any visual output when I load my test page.

    Here's the source:

    HTML Source (index.htm)
    Code:
    <html>
      <head>
        <title>Test</title>
      </head>
    
      <body>
        <script type="text/javascript">
          function loadXMLDoc(filename) {
            if(window.XMLHttpRequest) {
              xhttp = new XMLHttpRequest();
            }
            else {
              xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
    
            xhttp.open("GET", filename, false);
            xhttp.send();
            return xhttp.responseXML;
          }
          xmlDoc = loadXMLDoc("member_list.xml");
          x = xmlDoc.getElementsByTagName("member");
    
          for(i = 0; i < x.length; i++) {
            for(j = 0; j < x[i].childNodes.length; j++) {
              documents.write(x[i].childNodes[j].nodeValue);
              documents.write("<br />");
            }
          }
        </script>
      </body>
    
    </html>
    XML Source (member_list.xml)
    Code:
    <?xml version="1.0" ?> 
    
    <!DOCTYPE member_list [
      <!ELEMENT member_list (member*)>
      <!ELEMENT member (id, ign+, first_name?, last_name?, email*, phone*)>
      <!ELEMENT id (#PCDATA)>
      <!ELEMENT ign (#PCDATA)>
      <!ELEMENT first_name (#PCDATA)>
      <!ELEMENT last_name (#PCDATA)>
      <!ELEMENT email (#PCDATA)>
      <!ELEMENT phone (#PCDATA)>
    
      <!ATTLIST ign type CDATA #IMPLIED>
      <!ATTLIST email type CDATA #IMPLIED>
      <!ATTLIST phone type CDATA #IMPLIED>
    ]>
    
      <member_list>
    
        <member>
          <id>MAT</id>
          <ign>Mattius</ign>
          <first_name>Michael</first_name>
          <last_name>Pennington</last_name>
          <email>NA</email>
          <phone>NA</phone>
        </member>
    
        <member>
          <id>BER</id>
          <ign type="primary">Bereham</ign>
          <ign type="secondary">Bearham</ign>
          <first_name>Donny</first_name>
          <last_name>Barham</last_name>
          <email>NA</email>
          <phone>NA</phone>
        </member>
    
        <member>
          <id>CHA</id>
          <ign>CHARRO</ign>
          <first_name>Michael</first_name>
          <last_name>Pennington</last_name>
          <email>NA</email>
          <phone>NA</phone>
        </member>
    
      </member_list>
    Please, if you can find the error that I am missing, do. It's probably something simple that I'm just not catching.
  2. #2
  3. No Profile Picture
    Masked Chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    Ohio/Pennsylvania
    Posts
    107
    Rep Power
    14
    Originally Posted by michael.penning
    Hello! I'm new to the community, so it's nice to meet all of you. My name is Michael and I'm an aspiring software engineer with roots in web development. I'm happy to have finally found a place where I can receive feedback and assistance on any code I may have a problem with or require feedback for.

    Okay, so, here's the issue:

    I have recently started experimenting with XML and XML DOM to try to add a little bit of dynamics to the process of exchanging and altering data on my sites. Even though that to my knowledge my syntax is straight and correct, I am still not receiving any visual output when I load my test page.

    Here's the source:

    HTML Source (index.htm)
    Code:
    <html>
      <head>
        <title>Test</title>
      </head>
    
      <body>
        <script type="text/javascript">
          function loadXMLDoc(filename) {
            if(window.XMLHttpRequest) {
              xhttp = new XMLHttpRequest();
            }
            else {
              xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
    
            xhttp.open("GET", filename, false);
            xhttp.send();
            return xhttp.responseXML;
          }
          xmlDoc = loadXMLDoc("member_list.xml");
          x = xmlDoc.getElementsByTagName("member");
    
          for(i = 0; i < x.length; i++) {
            for(j = 0; j < x[i].childNodes.length; j++) {
              documents.write(x[i].childNodes[j].nodeValue);
              documents.write("<br />");
            }
          }
        </script>
      </body>
    
    </html>
    XML Source (member_list.xml)
    Code:
    <?xml version="1.0" ?> 
    
    <!DOCTYPE member_list [
      <!ELEMENT member_list (member*)>
      <!ELEMENT member (id, ign+, first_name?, last_name?, email*, phone*)>
      <!ELEMENT id (#PCDATA)>
      <!ELEMENT ign (#PCDATA)>
      <!ELEMENT first_name (#PCDATA)>
      <!ELEMENT last_name (#PCDATA)>
      <!ELEMENT email (#PCDATA)>
      <!ELEMENT phone (#PCDATA)>
    
      <!ATTLIST ign type CDATA #IMPLIED>
      <!ATTLIST email type CDATA #IMPLIED>
      <!ATTLIST phone type CDATA #IMPLIED>
    ]>
    
      <member_list>
    
        <member>
          <id>MAT</id>
          <ign>Mattius</ign>
          <first_name>Michael</first_name>
          <last_name>Pennington</last_name>
          <email>NA</email>
          <phone>NA</phone>
        </member>
    
        <member>
          <id>BER</id>
          <ign type="primary">Bereham</ign>
          <ign type="secondary">Bearham</ign>
          <first_name>Donny</first_name>
          <last_name>Barham</last_name>
          <email>NA</email>
          <phone>NA</phone>
        </member>
    
        <member>
          <id>CHA</id>
          <ign>CHARRO</ign>
          <first_name>Michael</first_name>
          <last_name>Pennington</last_name>
          <email>NA</email>
          <phone>NA</phone>
        </member>
    
      </member_list>
    Please, if you can find the error that I am missing, do. It's probably something simple that I'm just not catching.
    well, for starters its 'document.write' not 'documents.write'. Also, how are you calling this javascript code to load?
    ____________
    Thanks,
    Skeasor

    Got Debian Linux?
    www.debian.org
  4. #3
  5. No Profile Picture
    Masked Chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    Ohio/Pennsylvania
    Posts
    107
    Rep Power
    14
    Also, take a look here...It's a pretty decent example:
    http://www.hiteshagrawal.com/javascript/javascript-parsing-xml-in-javascript
    ____________
    Thanks,
    Skeasor

    Got Debian Linux?
    www.debian.org
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0
    Ha! See, something simple!

    I change "documents." to "document.".
    It is now displaying data; however, it's not the correct data.
    It has decided to now print "null" in place of the actual nodeValue.

IMN logo majestic logo threadwatch logo seochat tools logo