Unable to parse XML structure using DOM library

Joined
Aug 20, 2008
Messages
1
Reaction score
0
Hi all,

I'm trying to parse this XML structure:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<parameter name="OfferMap" type="Map">
	<parameter name="OtherMap" type="Map">
	</parameter>
	<parameter name="YetAnotherMap" type="Map">
	</parameter>
	<parameter name="Contract" type="Map">
		<parameter name="OtherMap" type="Map">
		</parameter>
		<parameter name="VoiceGenOfferInfo" type="Map">
			<parameter name="something" type="String">214</parameter>
			<parameter name="something" type="String">214</parameter>
			<parameter name="something" type="String">214</parameter>
			<parameter name="something" type="String">214</parameter>
			<parameter name="something" type="String">214</parameter>
			<parameter name="something" type="String">214</parameter>
			<parameter name="ServicesToActivate" type="Array">
				<parameter type="Map">
					<parameter name="ServicePackage" type="String">577</parameter>
					<parameter name="service" type="String">1954</parameter>
				</parameter>
				<parameter type="Map">
					<parameter name="ServicePackage" type="String">577</parameter>
					<parameter name="service" type="String">1955</parameter>
				</parameter>
			</parameter>
		</parameter>
	</parameter>
</parameter>

but the following line:
Code:
initialDocument.getChildNodes().item(0).getChildNodes().item(5).getChildNodes().item(3).getChildNodes().item(13)

where initialDocument represents a DOMDocument with the XML posted above, only returns:

Code:
<?xml version="1.0"?>
<parameter name="ServicesToActivate" type="Array">
	<parameter type="Map">
		<parameter name="ServicePackage" type="String">577</parameter>
		<parameter name="service" type="String">1955</parameter>
	</parameter>
</parameter>

What am i doing wrong? Why does the first map disapear (the one were the service is 1954)?

How can i obtain the values for both the vars servicepackage and service?

TIA
 
Joined
Aug 22, 2008
Messages
6
Reaction score
0
Sometimes DOM interprets the white space in an XML file as if they were TEXT nodes, instead of the visible ELEMENT nodes wee see and expect.

So, instead of doing the long nested item().getChildNodes().item,
maybe break it up to nested blocks where

Code:
NodeList n1 = initialDocument.getChildNodes();
for (int i = 0; i < n1.getLength(); i++) {
 Node n = n1.item(i);
 if (n.getNodeType == Node.ELEMENT_NODE) {
   NodeList n2 = n.getChildNodes();
   // and so on.
 }
}

Another option, if you are able to, have you tried out the StAX API? It is a bit more elegant for reading XML files, and is bundled into Java 1.6
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top