1

I have an HTML file with Javascript in it. But I am not able to load this HTML file using tinyXML2 library. It is giving error.

My html file is like abc.html:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <title>HTML Course</title>
  <style type="text/css">
ul.LinkedList { display: block; }
/* ul.LinkedList ul { display: none; } */
.HandCursorStyle { cursor: pointer; cursor: hand; }  /* For IE */
  </style>

  <script type="text/JavaScript">
function addEvents() {
      activateTree(document.getElementById("LinkedList1"));
    }
    function activateTree() 
 {
       for(var i=0; i < oList.getElementsByTagName("ul").length; i++) 
    {
        oList.getElementsByTagName("ul")[i].style.display="none";            
      }                                                                  
       if(oList.addEventListener) 
    {
        oList.addEventListener("click", toggleBranch, false);
      }
   else if(oList.attachEvent) 
   { 
        oList.attachEvent("onclick", toggleBranch);
      }
       addLinksToBranches(oList);
    }
  </script>
</head>

 <body >
<ul id="LinkedList1" class="LinkedList">
  <li>History of WWW
    <ul>
      <li>Arpanet - Packets - 1969</li>
      <li>TCP/IP - Vinton Cerf - 1974</li>
      <li>WorldWideWeb (Internet and program) - Tim Berners Lee - 1991</li>
      <li>Public Domain WWW source code - 1993</li>
      <li>NCSA Mosaic released - 1993</li>
      <li>Opera released - 1994</li>
      <li>Marc Anderseen (formerly NCSA) and Jim Clark  release Netscape - 1994</li>
      <li>IE from Microsoft (based on Mosaic)
        <ul>
          <li>DHTML</li>
          <li>ActiveX</li>
        </ul>
      </li>
      <li>W3C at MIT (CERN, NCSA, EU)</li>
      <li>W3C Recommendations</li>
      <li>Mozilla Foundation</li>
    </ul>
  </li>

</ul>
</body>
</html>

and my c++ code to load this html file is:

tinyxml2::XMLDocument xmlDoc;
tinyxml2::XMLError err =  xmlDoc.LoadFile("abc.html");

Now err has the error code XML_ERROR_PARSING_ELEMENT.

What I want to do this HTML file is to find a particular tag say <ul> under <body> tag and want to add some more items inside that tag.

Please let me know why it is giving error and how can I do it in a way or not.

Garf365
  • 3,619
  • 5
  • 29
  • 41

1 Answers1

1

This line is your problem:

       for(var i=0; i < oList.getElementsByTagName("ul").length; i++) 

The less-than comparison is interpreted by the XML parser as the start of an element, but since it's not followed by a legal element name, the parser freaks out and dies on you, as is its wont.

Wrap the contents of the script tag in a <![CDATA[]]> section; escape it using &lt;, or flip the comparison making it a >= instead.

    <body >

Might also cause problems due to the space after the element name, although it shouldn't. (It's valid XML, but not every parser is entirely compliant.)

Edit:

As Garf points out; it's good to separate XML and JS, as well as CSS, and this is precisely the reason.

What you will want in XML serialization is usually this:

<script type="text/javascript" src="myScript.js" />
Williham Totland
  • 28,471
  • 6
  • 52
  • 68
  • In addition to this answer, I suggest to separate XML and javascript, each in their own file, if it's possible – Garf365 Jun 20 '16 at 08:01
  • @Williham Totland, my HTML file loaded successfully through tinyXML2 library when I used <![CDATA[]]>. I want to one more thing how to find a particular tag using tinyXML2. Is there any way to find? I didn't get any help for finding a particular tag in grinningLizards website. – Britton Gary Jun 20 '16 at 08:42
  • @Britton Then asking another question is probably the way to go. :) – Williham Totland Jun 20 '16 at 08:43
  • @WillihamTotland, Thanks a lot for helping out. :) :P (y) – Britton Gary Jun 20 '16 at 08:48