Britton Britton - 10 months ago 37
HTML Question

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


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="" xml:lang="en" lang="en">
<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 */

<script type="text/JavaScript">
function addEvents() {
function activateTree()
for(var i=0; i < oList.getElementsByTagName("ul").length; i++)
oList.addEventListener("click", toggleBranch, false);
else if(oList.attachEvent)
oList.attachEvent("onclick", toggleBranch);

<body >
<ul id="LinkedList1" class="LinkedList">
<li>History of WWW
<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)
<li>W3C at MIT (CERN, NCSA, EU)</li>
<li>W3C Recommendations</li>
<li>Mozilla Foundation</li>


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

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

has the error code

What I want to do this HTML file is to find a particular tag say
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.


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.)


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