For previously been discussed about how to convert data to xml, we now discuss for how to parse xml. This is a simple way for parsing the xml file.
The following example code :
XML File :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table> | |
− | |
<Row> | |
<kecamatan> KARANG PILANG </kecamatan> | |
<laki>74</laki> | |
</Row> | |
− | |
<Row> | |
<kecamatan> WONOCOLO </kecamatan> | |
<laki>69</laki> | |
</Row> | |
− | |
<Row> | |
<kecamatan> RUNGKUT </kecamatan> | |
<laki>115</laki> | |
</Row> | |
− | |
<Row> | |
<kecamatan> WONOKROMO </kecamatan> | |
<laki>138</laki> | |
</Row> | |
</table> |
Output :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Root element table | |
Total no of people : 5 | |
Kecamatan : KARANG PILANG | |
Laki-Laki : 74 | |
Kecamatan : WONOCOLO | |
Laki-Laki : 69 | |
Kecamatan : RUNGKUT | |
Laki-Laki : 115 | |
Kecamatan : WONOKROMO | |
Laki-Laki : 138 | |
Kecamatan : TEGALSARI | |
Laki-Laki : 60 |
Java Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("c:/cobaya.xml")); //path of file XML | |
doc.getDocumentElement ().normalize (); | |
System.out.println ("Root element " + | |
doc.getDocumentElement().getNodeName()); | |
NodeList listOfPersons = doc.getElementsByTagName("Row"); | |
int totalPersons = listOfPersons.getLength(); | |
System.out.println("Total no of people : " + 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("kecamatan"); //node | |
Element firstNameElement = (Element)firstNameList.item(0); | |
NodeList textFNList = firstNameElement.getChildNodes(); | |
System.out.println("Kecamatan : " + | |
((Node)textFNList.item(0)).getNodeValue().trim()); | |
//------- | |
NodeList lastNameList = firstPersonElement.getElementsByTagName("laki"); //Node | |
Element lastNameElement = (Element)lastNameList.item(0); | |
NodeList textLNList = lastNameElement.getChildNodes(); | |
System.out.println("Laki-Laki : " + | |
((Node)textLNList.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 | |
} |
There are better implementations of reading XML files which would work for any XML file. The above one would require a few changes every time the XML tag names change. But this is much more simpler than the other programs.
No comments:
Post a Comment