© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
Quick Links
Easy Studies

Parsing the XMLfile and store the contents in a StringBuffer Using Java


XML Parser is a piece of software whose job description is to check that an XML document is valid or, failing that, well-formed.This program takes the XML file as input.It parses the XML file from its root node, until it reaches the last child node. After parsing the file it store every node values in a StringBuffer. This program is invoked from ParseXMLFile() constructor and it will call the parseFile() method. After checking the order of the XML file, it calls the parse() method and the details are read from it and stored into the StringBuffer and it is displayed on the console.

Parsing the XMLfile and store the contents in a StringBuffer Using Java :

import java.io.*; import org.w3c.dom.*; import org.xml.sax.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; public class ParseXMLFile { private final static String xmlFileName = "c:/XML/Poorvika.xml"; StringBuffer sb=new StringBuffer(); StringBuffer sb1=new StringBuffer(); public ParseXMLFile() { Document doc = parseFile1(xmlFileName); Node root = doc.getDocumentElement(); System.out.println("Statemend of XML document..."); writeDocumentToOutput(root,0); System.out.println("... end of statement"); } public final static String getElementValue( Node elem ) { Node kid; if( elem != null){ if (elem.hasChildNodes()){ for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){ if( kid.getNodeType() == Node.TEXT_NODE ){ return kid.getNodeValue(); } } } } return ""; } private String getIndentSpaces(int indent) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < indent; i++) { buffer.append(" "); } return buffer.toString(); } public void writeDocumentToOutput(Node node,int indent) { String nodeName = node.getNodeName(); String nodeValue = getElementValue(node); sb.append(nodeValue); if(nodeValue.equals("")){ }else{ sb.append("#"); } NamedNodeMap attributes = node.getAttributes(); System.out.println(getIndentSpaces(indent) + "NodeName: " + nodeName + ", NodeValue: " + nodeValue); NodeList children = node.getChildNodes(); for (int i = 0;i

Sample ScreenShot:

SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑