Solved

java read xml with complex tags

Posted on 2013-01-02
7
1,032 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 45
servlet example 11 49
servlet filter example 37 71
Selenium docs api java index 3 48
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

790 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