Connecting Tech Pros Worldwide Help | Site Map

How to get the attribute from the self closing tag

Newbie
 
Join Date: Sep 2009
Posts: 4
#1: Sep 9 '09
Let me give you my problem statement:

Expand|Select|Wrap|Line Numbers
  1. <positiondata >
  2. <touchpad reportedFingers="2" />
  3.   <timestamp user="Dev" time="1252088979018" />
  4.   <reported2d x="447" y="232" z="31" w="1" finger="0" fingerPresent="false" />
  5.   <fingercount count="1" timestamp="1252088992144" />
  6.   <reported2d x="400" y="240" z="32" w="1" finger="0" fingerPresent="false" />
  7.   <reported2d x="353" y="247" z="32" w="1" finger="0" fingerPresent="false" />
  8.   <reported2d x="317" y="250" z="33" w="1" finger="0" fingerPresent="false" />
  9.   <reported2d x="304" y="257" z="33" w="1" finger="0" fingerPresent="false" />
  10. <fingercount count="0" timestamp="1252089104305" />
  11. </positiondata>
above is my sort xml file and i want to parse x,y,z value in the array and want to use it for statical analysis. If somebody help me to parse above xml file with Java or any other language then it will be good.

I try to write the java program, but i don't know how to get the attribute from self contain tag.
for example:
Expand|Select|Wrap|Line Numbers
  1.  <?xml version="1.0" ?> 
  2. - <book>
  3. - <person>
  4.   <first>x="Kiran shah"</first> 
  5.   <last>Pai</last> 
  6.   <age>22</age> 
  7.   </person>
  8. - <person>
  9.   <first>Bill</first> 
  10.   <last>Gates</last> 
  11.   <age>46</age> 
  12.   </person>
  13. - <person>
  14.   <first>Steve</first> 
  15.   <last>Jobs</last> 
  16.   <age>40</age> 
  17.   </person>
  18.   </book>
to read above xml file, the java program that is written is below and it is pretty simple:

Expand|Select|Wrap|Line Numbers
  1. import java.io.File;
  2. import org.w3c.dom.Document;
  3. import org.w3c.dom.*; 
  4.  
  5. import javax.xml.parsers.DocumentBuilderFactory;
  6. import javax.xml.parsers.DocumentBuilder;
  7. import org.xml.sax.SAXException;
  8. import org.xml.sax.SAXParseException; 
  9.  
  10. public class ReadAndPrintXMLFile{
  11.  
  12. public static void main (String argv []){
  13. try {
  14.  
  15. DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  16. DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
  17. Document doc = docBuilder.parse (new File("book.xml"));
  18.  
  19. // normalize text representation
  20. doc.getDocumentElement ().normalize ();
  21. System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
  22.  
  23. NodeList listOfPersons = doc.getElementsByTagName("person");
  24. int totalPersons = listOfPersons.getLength();
  25. System.out.println("Total no of nodes : " + totalPersons);
  26.  
  27. for(int s=0; s<listOfPersons.getLength() ; s++){
  28.  
  29. Node firstPersonNode = listOfPersons.item(s);
  30. if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
  31.  
  32. Element firstPersonElement = (Element)firstPersonNode; 
  33.  
  34. //-------
  35. NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
  36. Element firstNameElement = (Element)firstNameList.item(0);
  37.  
  38. NodeList textFNList = firstNameElement.getChildNodes();
  39. System.out.println("First Name : " + ((Node)textFNList.item(0)).getNodeValue().trim());
  40.  
  41. //------- 
  42. NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
  43. Element lastNameElement = (Element)lastNameList.item(0);
  44.  
  45. NodeList textLNList = lastNameElement.getChildNodes();
  46. System.out.println("Last Name : " + ((Node)textLNList.item(0)).getNodeValue().trim());
  47.  
  48. //----
  49. NodeList ageList = firstPersonElement.getElementsByTagName("age");
  50. Element ageElement = (Element)ageList.item(0);
  51.  
  52. NodeList textAgeList = ageElement.getChildNodes();
  53. System.out.println("Age : " + ((Node)textAgeList.item(0)).getNodeValue().trim());
  54.  
  55. //------
  56.  
  57. }//end of if clause
  58.  
  59. }//end of for loop with s var
  60.  
  61. }catch (SAXParseException err) {
  62. System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
  63. System.out.println(" " + err.getMessage ());
  64.  
  65. }catch (SAXException e) {
  66. Exception x = e.getException ();
  67. ((x == null) ? e : x).printStackTrace ();
  68.  
  69. }catch (Throwable t) {
  70. t.printStackTrace ();
  71. }
  72. //System.exit (0);
  73.  
  74. }//end of main
  75.  
  76. }

So i don't know how to parse the self closing tag's attribute. Any quickly help would be appreciated.

-Krunal
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,635
#2: Sep 10 '09

re: How to get the attribute from the self closing tag


you can use XPath or DOM to get the attribute values, though I'm not familiar with their Java implementation (looks like you’re already using DOM).

DOM should have something like getAttribute(), check out the documentation (either at W3C or at the Java Docs).

besides that, your second XML does not contain any attributes.
Reply

Tags
java, self closing tags, xml