Let me give you my problem statement:
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:
to read above xml file, the java program that is written is below and it is pretty simple:
So i don't know how to parse the self closing tag's attribute. Any quickly help would be appreciated.
-Krunal
Code:
<positiondata > <touchpad reportedFingers="2" /> <timestamp user="Dev" time="1252088979018" /> <reported2d x="447" y="232" z="31" w="1" finger="0" fingerPresent="false" /> <fingercount count="1" timestamp="1252088992144" /> <reported2d x="400" y="240" z="32" w="1" finger="0" fingerPresent="false" /> <reported2d x="353" y="247" z="32" w="1" finger="0" fingerPresent="false" /> <reported2d x="317" y="250" z="33" w="1" finger="0" fingerPresent="false" /> <reported2d x="304" y="257" z="33" w="1" finger="0" fingerPresent="false" /> <fingercount count="0" timestamp="1252089104305" /> </positiondata>
I try to write the java program, but i don't know how to get the attribute from self contain tag.
for example:
Code:
<?xml version="1.0" ?> - <book> - <person> <first>x="Kiran shah"</first> <last>Pai</last> <age>22</age> </person> - <person> <first>Bill</first> <last>Gates</last> <age>46</age> </person> - <person> <first>Steve</first> <last>Jobs</last> <age>40</age> </person> </book>
Code:
import java.io.File; import org.w3c.dom.Document; import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; public class ReadAndPrintXMLFile{ public static void main (String argv []){ try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse (new File("book.xml")); // normalize text representation doc.getDocumentElement ().normalize (); System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName()); NodeList listOfPersons = doc.getElementsByTagName("person"); int totalPersons = listOfPersons.getLength(); System.out.println("Total no of nodes : " + totalPersons); for(int s=0; s<listOfPersons.getLength() ; s++){ Node firstPersonNode = listOfPersons.item(s); if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){ Element firstPersonElement = (Element)firstPersonNode; //------- NodeList firstNameList = firstPersonElement.getElementsByTagName("first"); Element firstNameElement = (Element)firstNameList.item(0); NodeList textFNList = firstNameElement.getChildNodes(); System.out.println("First Name : " + ((Node)textFNList.item(0)).getNodeValue().trim()); //------- NodeList lastNameList = firstPersonElement.getElementsByTagName("last"); Element lastNameElement = (Element)lastNameList.item(0); NodeList textLNList = lastNameElement.getChildNodes(); System.out.println("Last Name : " + ((Node)textLNList.item(0)).getNodeValue().trim()); //---- NodeList ageList = firstPersonElement.getElementsByTagName("age"); Element ageElement = (Element)ageList.item(0); NodeList textAgeList = ageElement.getChildNodes(); System.out.println("Age : " + ((Node)textAgeList.item(0)).getNodeValue().trim()); //------ }//end of if clause }//end of for loop with s var }catch (SAXParseException err) { System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ()); System.out.println(" " + err.getMessage ()); }catch (SAXException e) { Exception x = e.getException (); ((x == null) ? e : x).printStackTrace (); }catch (Throwable t) { t.printStackTrace (); } //System.exit (0); }//end of main }
So i don't know how to parse the self closing tag's attribute. Any quickly help would be appreciated.
-Krunal
Comment