Unable to parse XML structure using DOM library

Discussion in 'Java' started by Viegas, Aug 20, 2008.

  1. Viegas

    Viegas

    Joined:
    Aug 20, 2008
    Messages:
    1
    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
    Viegas, Aug 20, 2008
    #1
    1. Advertising

  2. Viegas

    travishein

    Joined:
    Aug 22, 2008
    Messages:
    6
    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
    travishein, Aug 22, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. laughlin
    Replies:
    1
    Views:
    499
    John Bokma
    Aug 14, 2003
  2. Harobed
    Replies:
    1
    Views:
    354
    Martin v. =?iso-8859-15?q?L=F6wis?=
    Sep 1, 2003
  3. Harobed
    Replies:
    0
    Views:
    327
    Harobed
    Sep 1, 2003
  4. Charlie T

    using DOM to parse a XML file...

    Charlie T, Sep 9, 2004, in forum: Javascript
    Replies:
    0
    Views:
    70
    Charlie T
    Sep 9, 2004
  5. Carles Company Soler

    Open a XML and parse it with DOM

    Carles Company Soler, Nov 23, 2006, in forum: Javascript
    Replies:
    1
    Views:
    72
    Martin Honnen
    Nov 23, 2006
Loading...

Share This Page