Solved

java read xml with complex tags

Posted on 2013-01-02
7
1,018 Views
Last Modified: 2013-01-03
I'm able unable to read complex tags of a xml

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:c14n="http://www.w3.org/2001/10/xml-exc-c14n#" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:diag="urn:SerenaDiagnostics" xmlns:ae="urn:sbmappservices72">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <ae:GetSubmitProjectsResponse>
         <ae:return>
            <ae:project xsi:type="ae:ProjectIdentifier">
               <ae:displayName>ALM Projects</ae:displayName>
               <ae:id>11</ae:id>
               <ae:uuid>d5b71b08-1c92-47fe-8513-1753e5c829b0</ae:uuid>
               <ae:fullyQualifiedName>Base Project||ALM Projects</ae:fullyQualifiedName>
               <ae:internalName>USR_ALM_PROJECTS.ALM_PROJECTS</ae:internalName>
            </ae:project>
            <ae:description/>
         </ae:return>
         <ae:return>
            <ae:project xsi:type="ae:ProjectIdentifier">
               <ae:displayName>URA</ae:displayName>
               <ae:id>48</ae:id>
               <ae:uuid>d85387bb-6a8e-46d1-a0ad-31abb8a4ff41</ae:uuid>
               <ae:fullyQualifiedName>Base Project||ALM Projects||URA</ae:fullyQualifiedName>
               <ae:internalName>USR_ALM_PROJECTS.URA</ae:internalName>
            </ae:project>
            <ae:description/>
         </ae:return>
      </ae:GetSubmitProjectsResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Open in new window


Document document = parseXmlFile(ret.ReturnMessage);
		NodeList listProjects = document.getElementsByTagName("ae:project xsi:type='ae:ProjectIdentifier'");
		_log.info("INF - Operations | enableRolesforSBMMultiUserFieldsonALMProject - length = " + listProjects.getLength());
		for(int x=0; x < listProjects.getLength(); x++){
 
		   Node nNode = (Node) listProjects.item(x);
		   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
		      Element eElement = (Element) nNode;
		      if (getTagValue("ae:displayName", eElement) == almProject.ALMProjectName){
		    	  almProject.ALMProjectSBMID = getTagValue("ae:id", eElement);
		    	  _log.info("INF - Operations | enableRolesforSBMMultiUserFieldsonALMProject - ProjectID = " + almProject.ALMProjectSBMID);
		      } 
		   }
		}

Open in new window


The output of the code:
02 Jan 2013 21:27:29 |  INFO [http-8080-1] - INF - Operations | enableRolesforSBMMultiUserFieldsonALMProject - length = 0

I should be able to read <ae:displayName> and <ae:id>
0
Comment
Question by:tcorigli
  • 4
  • 3
7 Comments
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 500 total points
ID: 38738305
Change line 2 of the above to...
		NodeList listProjects = document.getElementsByTagNameNS("urn:sbmappservices72", "project");

Open in new window

And you haven't posted the code for your getTagValue() method, but you will probably have to change that too, to handle namespaces properly like what I have just posted above!
0
 

Author Comment

by:tcorigli
ID: 38739246
Hi mccarl,

I applied the code change but I'm still receiving 0 as a length of listProjects

The code of getTagValue:

Open in new window

private static String getTagValue(String sTag, Element eElement) {
		NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
	 
	        Node nValue = (Node) nlList.item(0);
	 
		return nValue.getNodeValue();
	}

Open in new window

0
 
LVL 35

Expert Comment

by:mccarl
ID: 38739977
Can you post the code for the parseXmlFile() method too?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:tcorigli
ID: 38739987
private Document parseXmlFile(String in) {
	    try {
	        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	        DocumentBuilder db = dbf.newDocumentBuilder();
	        InputSource is = new InputSource(new StringReader(in));
	        return (Document) db.parse(is);
	    } catch (ParserConfigurationException e) {
	        throw new RuntimeException(e);
	    } catch (SAXException e) {
	        throw new RuntimeException(e);
	    } catch (IOException e) {
	        throw new RuntimeException(e);
	    }
	}

Open in new window

0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 38740503
In between lines 3 & 4 above (in parseXmlFile method), add the following line...
dbf.setNamespaceAware(true);

Open in new window

0
 

Author Comment

by:tcorigli
ID: 38740664
Thanks a lot mccarl, that did the trick.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 38741974
No worries, glad to help! :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now