473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to get the attribute from the self closing tag

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
Sep 9 '09 #1
1 7265
Dormilich
8,658 Expert Mod 8TB
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.
Sep 10 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a...
6
by: Coral Snake | last post by:
I am having problems with programming even simple "Hello World" programs from books and tutorials that use Python GUI libraries. Such Programs cause python to throw "Attribute Errors" even when the...
6
by: Alex Hunsley | last post by:
I know that I can catch access to unknown attributes with code something like the following: class example: def __getattr__(self, name): if name == 'age': return __age else: raise...
5
by: Russell Warren | last post by:
I just ran across a case which seems like an odd exception to either what I understand as the "normal" variable lookup scheme in an instance/object heirarchy, or to the rules regarding variable...
3
by: Eric Mahurin | last post by:
Is there a standard way to get a descriptor object for an arbitrary object attribute - independent of whether it uses the descriptor/ property protocol or not. I want some kind of...
6
by: Adam Donahue | last post by:
As an exercise I'm attempting to write a metaclass that causes an exception to be thrown whenever a user tries to access 'attributes' (in the traditional sense) via a direct reference. Consider:...
13
by: globalrev | last post by:
wassup here? 7 Traceback (most recent call last): File "C:\Python25\myPrograms\netflix\netflix.py", line 22, in <module> print cust1.getID() AttributeError: 'NoneType' object has no...
18
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work,...
0
by: Terry Reedy | last post by:
"Casey McGinty" <casey.mcginty@gmail.comwrote in message news:ae3f58470806051341s75052c29s7adcc4c994462618@mail.gmail.com... | On Thu, Jun 5, 2008 at 5:40 AM, Gabriel Rossetti < |...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.