Search code examples
htmlc++11tinyxml2

Load HTML file with javascript using tinyXML2 library


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.


Solution

  • 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" />