Must all parent node have the same types of childs?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Kypriotis
    New Member
    • Mar 2011
    • 37

    Must all parent node have the same types of childs?

    For example, I want a list of banners, all will have a name, but not all say a url can I write this
    Code:
    <list>
    <banner>
    <name>a.jpg<name>
    </banner>
    <banner>
    <name>a.jpg<name>
    <url>http://yahoo.com</url>
    </banner>
    </list>
    is this valid?
    P.S. I do not want to include an empty <url> child to all <banner> since that would be a waste and I do not want to rename the <banner> with <url> to some thing else
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    it is valid in the sense as it follows the XML base rules (given that you use UTF-8).

    for stricter validaty, you’d need a DTD or Schema (XSD or RelaxNG)

    Comment

    • Mike Kypriotis
      New Member
      • Mar 2011
      • 37

      #3
      aside from the DTD/UTF-8 because everything I have seen up to now sticks with the same childs in every parent node with the same name, is there some downside/disadvantange with this logic?
      (When extracting data in my for loop I plan to use a simple if to see wheather the <url> child exists or not in each parent)

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I think it’s a matter of personal preference. if you want only the URLs, use XPath or getElementsByTa gName().

        Comment

        • Mike Kypriotis
          New Member
          • Mar 2011
          • 37

          #5
          All the job will be done with JS. Know any good tutorial?
          (Also for reading an xml file, the only option is AJAX? Because the point of the whole thing is to make an application which works offline on the browser-thus the javascript)

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            without help of a server side script, AJAX is the only option in mind for reading XML in a HTML page.

            Comment

            • Mike Kypriotis
              New Member
              • Mar 2011
              • 37

              #7
              following the original problem I need to check wheather a parent node has a child node <data> (some will and some will not) trying
              Code:
              if (typeof(x[i].getElementsByTagName("data")[0].childNodes[0].nodeValue)== "undefined")  
                data='none';
                else
                data=x[i].getElementsByTagName("data")[0].childNodes[0].nodeValue;
              where x[i] the current parent element within the loop but still get an error that x[i].getElementsByT agName("data")[0].childNodes[0].nodeValue is undefined, note that if the parent has this node everything is works ok, so x[i].getElementsByT agName("data")[0].childNodes[0].nodeValue gets the value if it finds it, any ideas?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                check the length of the getElementsByTa gName() list. childNodes[0].nodeValue is undefined if there is either no child node (empty tag) or no parent node (no parent, no child)

                Comment

                • Mike Kypriotis
                  New Member
                  • Mar 2011
                  • 37

                  #9
                  worked well, with this I can check the existance of data. The other think I want to do is check the overall xml file for syntax errors. Because a admin will upload it, say he deleted a '<' when changing and now a node is not closed properly do you now any way to check it?

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    invalid XML doesn’t load.

                    Comment

                    • Mike Kypriotis
                      New Member
                      • Mar 2011
                      • 37

                      #11
                      found this from w3schools and seems to work, will dig to see how it is done.
                      P.S. You load the xml with the way shown below or with AJAX like any other data like this ?
                      Code:
                      function loadXMLDoc(dname)
                      {
                      if (window.XMLHttpRequest)
                        {
                        xhttp=new XMLHttpRequest();
                        }
                      else
                        {
                        xhttp=new ActiveXObject("Microsoft.XMLHTTP");
                        }
                      xhttp.open("GET",dname,false);
                      xhttp.send();
                      return xhttp.responseXML;
                      }
                      Code:
                      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                      <html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
                      <head>
                      <title>DOM Tutorial - Validate XML</title>
                      </head>
                      <body>
                      
                      <script language="JavaScript" type="text/javascript">
                      document.write("<h2>Result of Validating: http://www.w3schools.com/dom/cd_catalog.xml</h2>");
                      // code for IE
                      if (window.ActiveXObject)
                      {
                      var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                      xmlDoc.async="false";
                      xmlDoc.validateOnParse="true";
                      xmlDoc.load("http://www.w3schools.com/dom/cd_catalog.xml");
                      
                      if (xmlDoc.parseError.errorCode == 0) 
                        {
                        document.write("The file <b>http://www.w3schools.com/dom/cd_catalog.xml</b><br />Validated Successfully without Errors");
                        }
                      else
                        {
                        document.write("<code>");
                        document.write("Error in line " + xmlDoc.parseError.line + " position " + xmlDoc.parseError.linePos);
                        document.write("<br /><br />");
                      
                        document.write("Error Code: " + xmlDoc.parseError.errorCode);
                        document.write("<br /><br />");
                      
                        document.write("Error Reason: " + xmlDoc.parseError.reason);
                        document.write("</code>");
                      
                        document.write("<xmp>");
                        document.write("Error Line: " + xmlDoc.parseError.srcText);
                        document.write("</xmp>");
                        }
                      }
                      // code for Mozilla, Firefox, Opera, etc.
                      else if (document.implementation && document.implementation.createDocument)
                      {
                      xmlDoc=document.implementation.createDocument("","",null);
                      xmlDoc.async=false;
                      xmlDoc.load("http://www.w3schools.com/dom/cd_catalog.xml");
                      
                      if (xmlDoc.documentElement.nodeName=="parsererror")
                        {
                        document.write(xmlDoc.documentElement.childNodes[0].nodeValue);
                        }
                      else
                        {
                        document.write("XML is valid");
                        }
                      }
                      else
                      {
                      document.write('Your browser cannot handle this script');
                      }
                      </script>
                      
                      </body>
                      </html>

                      Comment

                      Working...