Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simple Java DOM Parser

843834May 1 2009 — edited May 1 2009
Hi, I wrote a sample java dom parser code for accessing XML. Can somebay write more simpler code, please add that in the post.

***XML***
<?xml version="1.0" encoding="UTF-8"?>
<portfolio>
<stock>
<symbol>SUNW</symbol>
<name>Sun Microsystems</name>
<price>17.1</price>
</stock>
<stock>
<symbol>AOL</symbol>
<name>America Online</name>
<price>51.05</price>
</stock>
<stock>
<symbol>IBM</symbol>
<name>International Business
Machines</name>
<price>116.10</price>
</stock>
<stock>
<symbol>MOT</symbol>
<name>MOTOROLA</name>
<price>15.20</price>
</stock>
</portfolio>

***Java***

package com.xml;


import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.sun.org.apache.xpath.internal.operations.Equals;

/** This class represents short example how to parse XML file,
* get XML nodes values and its values.<br><br>
* @author Anand
*/

public class DOMXMLParser {

private final static String xmlFileName = "StockPrice.xml";
/** Creates a new instance of ParseXMLFile */
public DOMXMLParser() {


}

/** Returns element value
* @param elem element (it is XML tag)
* @return Element value otherwise empty String
*/

public Document parseFile(String fileName) {
System.out.println("Parsing XML file... " + fileName);
DocumentBuilder docBuilder;
Document doc = null;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
}
catch (ParserConfigurationException e) {
System.out.println("Wrong parser configuration: " + e.getMessage());
return null;
}
File sourceFile = new File(fileName);
try {
doc = docBuilder.parse(sourceFile);
}
catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
}
catch (IOException e) {
System.out.println("Could not read source file: " + e.getMessage());
}
System.out.println("XML file parsed");
return doc;
}
public static void main(String[] args) {

DOMXMLParser xmla=new DOMXMLParser();
// parse XML file -> XML document will be build
Document doc = xmla.parseFile(xmlFileName);

NodeList x=doc.getElementsByTagName("stock");
System.out.println(x.getLength());

//System.out.println(x.item(0).getTextContent());
for (int i=0; i<x.getLength();i++){
//System.out.println(x.item(i).getChildNodes().item(1).getTextContent());
if ((x.item(i).getChildNodes().item(1).getTextContent()).equals("MOT") ){

System.out.println(x.item(i).getChildNodes().item(5).getTextContent());
}

}

// System.out.println(x.item(3).getChildNodes().item(1).getNodeName());
// System.out.println(x.item(3).getChildNodes().item(1).getTextContent());
// System.out.println(x.item(3).getChildNodes().item(3).getNodeName());
// System.out.println(x.item(3).getChildNodes().item(3).getTextContent());
// System.out.println(x.item(3).getChildNodes().item(5).getNodeName());
// System.out.println(x.item(3).getChildNodes().item(5).getTextContent());
// System.out.println(doc.getElementsByTagName("stock").item(2).getFirstChild().getNodeValue());
}


}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 29 2009
Added on May 1 2009
1 comment
238 views