Solved

java read xml with complex tags

Posted on 2013-01-02
7
1,044 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 91
web project error add remove 1 85
Setup GlassFish 4 57
import as existing maven project 3 78
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

710 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