xmldoc.getElementsByTagName("pspagetitle")[x] is undefined

The following code is giving me the error “xmldoc.getElementsByTagName(“pspagetitle”) is undefined”. Having said that I can not for the life of me figure it out. I have been staring at this for two hours and I decided to put my ego aside and ask for some help. I know that this something simple I just can’t put my finger on it. So if there are any gurus out there that can help me it would be appreciated.

Note: The initial request for the document is working fine, if I place an alert alert(typeof root); it returns object and alert(root); returns [object HTMLCollection]. I have also included a copy of my XML document (via inline comment) that is being loaded via XMLHttpRequest()


var xmldoc = http_request.responseXML;
root = xmldoc.getElementsByTagName(“portalsettings”);

alert(typeof root); // <– returns object
alert(root); // <– returns [object HTMLCollection]

for(x = 0; x <= root.length; x++)
{
/*
<portal>
<productinfo>
<!-- Product Info Should be Set In This Element –>
<prodline>My Product Line Name</prodline>
<prodserial>V35-OOUN74DH7</prodserial>
<prodmodel>135</prodmodel>
<prodmanfdate>00.00.0000</prodmanfdate>
</productinfo>
<portalsettings>
<!-- Portal Information –>
<pspagetitle>Login Portal</pspagetitle>
<pscustomername>Customer Name</pscustomername>
<!-- Video Recorder Settings –>
<psheadend>
<headendurl>http://www.video.someniftyurlthatishidden.com</headendurl>
</psheadend>
</portalsettings>
<portal>
*/

    // The following line is the one causing my eventual brain aneurysm.
var m_pageTitle      = xmldoc.getElementsByTagName("pspagetitle")[x].childNodes[0].nodeValue;
var m_customerName   = xmldoc.getElementsByTagName("pscustomername")[x].childNodes[0].nodeValue;

//Not entirely sure this next line is right.. I have not been able to get past the original problem.
var m_psHeadEndUrl   = xmldoc.getElementsByTagName("psheadend")[x].childNodes[0].childNodes[0].nodeValue;

$('#highlight-black').append('&lt;strong&gt;' + m_pageTitle + '&lt;/strong&gt;');
document.title = m_pageTitle;
alert(m_psHeadEndUrl);

}


Thank you,
John

for(x = 0; x <= root.length; x++)
Which element of
xmldoc.getElementsByTagName(“pspagetitle”) will be referenced when x==root.length ?

After doing some additional research I found that getElementsByTagName returns 0 if the specified tag does not exist or could not be found. After finding this little tidbit of info I have modified my code to read.


for(x = 0; x < root.length; x++)

// Always returns 0
root = xmldoc.getElementsByTagName("portalinfo");

I have restructured my XML file slightly the new version is as follows, my problem is that I can read all of the data from the <videosources> node and all of the child nodes but not anything in the <portalsettings> node or any of the child nodes.


<?xml version="1.0" encoding="ISO-8859-1"?>
<portal>
	<portalsettings>
		<productinfo>
			<!-- Product Info Should be Set In This Element -->
			<prodline>Product Line</prodline>
			<prodserial>V35-OOUN74DH7</prodserial>
			<prodmodel>135</prodmodel>
			<prodmanfdate>00.00.0000</prodmanfdate>
		</productinfo>
		<portalinfo>
			<!-- Portal Information -->
			<pspagetitle>My Portal</pspagetitle>
			<pscustomername>Customer Name</pscustomername>
			<!-- Video Recorder Settings -->
			<psheadend>
				<headendurl>http://www.video.obscureurlreference.com</headendurl>
			</psheadend>
		</portalinfo>
	</portalsettings>
	<videosources>
		<!-- Each location will have it's own element, there may be 1 or more per site -->
		<source camera="0">
			<locationname>Location 1</locationname>
			<loclat>36.0158</loclat>
			<loclong>-78.5193</loclong>
			<locelev>35.0</locelev>
			<!-- EncType	0 = Axis Media Encoder		1 = Panasonic Encoder		2 = Bosch Encode	3 = Misc	-->
			<enctype>3</enctype>
			<encsnapshotaddress count="6" res="half" x0="0" y0="0" x1="2048" y1="1680" quality="15">http://10.41.12.101/image?</encsnapshotaddress>
			<encliveviewaddress>http://10.41.12.101/</encliveviewaddress>
			<encembedviewaddress>http://10.41.12.101/test</encembedviewaddress>
		</source>
		<source camera="1">
			<locationname>Location 2</locationname>
			<loclat>36.0158</loclat>
			<loclong>-78.5193</loclong>
			<locelev>35.0</locelev>
			<!-- EncType	0 = Axis Media Encoder		1 = Panasonic Encoder		2 = Bosch Encode	3 = Misc	-->
			<enctype>0</enctype>
			<encsnapshotaddress count="null">http://10.41.12.102/axis-cgi/jpg/image.cgi</encsnapshotaddress>
			<encliveviewaddress>https://10.41.12.102/view/viewer_index.shtml</encliveviewaddress>
			<encembedviewaddress>rtsp://10.41.12.102/axis-media/media.amp</encembedviewaddress>
		</source>
</videosources>
</portal>


The root node is <portal>, and there will only ever be one root node, so you don’t need a loop for it. For code clarity, I wouldn’t use a variable named “root” to refer to anything except the real root node.


//same as xmldoc.getElementsByTagName("portal").item(0);
var root = xmldoc.documentElement;

Now, you should be able traverse to anywhere in the document. Just start from the root and drill down as needed.


alert(root.getElementsByTagName("portalinfo").length);

I would strongly recommend you use jquery to help traverse the xml document though. It’s much less painful.

crmalibu - Thank you for the advice, I have since converted my existing code to use jQuery and I must say the ease of use and the sheer elegance of the code and readability are superb. Thank you for the suggestion, I do believe that I will be using jQuery for the rest of my projects that require client side processing.

Many Thanks,
John