Solved

java read xml with complex tags

Posted on 2013-01-02
7
1,010 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

706 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

20 Experts available now in Live!

Get 1:1 Help Now