Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java read xml with complex tags

Posted on 2013-01-02
7
Medium Priority
?
1,054 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 2000 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 36

Expert Comment

by:mccarl
ID: 38739977
Can you post the code for the parseXmlFile() method too?
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

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 36

Accepted Solution

by:
mccarl earned 2000 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 36

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

722 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