Getting Data from XML file

Getting Data from XML file

Page 1 of 1

1 Replies - 10521 Views - Last Post: 19 July 2008 - 12:32 PM Rate Topic: -----



Getting Data from XML file

Post icon  Posted 25 June 2008 - 11:11 AM

here is code:
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.xpath.*;

public class Main {

  public static void main(String[] args) 
   throws ParserConfigurationException, SAXException, 
		  IOException, XPathExpressionException {

	DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
	domFactory.setNamespaceAware(true); // never forget this!
	DocumentBuilder builder = domFactory.newDocumentBuilder();
	Document doc = builder.parse("c://database/teachers.xml");

	XPathFactory factory = XPathFactory.newInstance();
	XPath xpath = factory.newXPath();
	XPathExpression expr 
	 = xpath.compile("/teachers/teacher[@id=1]");
	String stringVal = (String)expr.evaluate(doc, XPathConstants.STRING);
	System.out.println("Got Value: " + stringVal);
	Object result = expr.evaluate(doc, XPathConstants.NODESET);
	NodeList nodes = (NodeList) result;
	for (int i = 0; i < nodes.getLength(); i++) {


and an xml file which i want to read and get some data from this xml file:

<?xml version="1.0" encoding="utf-8" ?> 
<teachers xmlns="" 
					  xsi:schemaLocation=" D:\XML\teachers.xsd"> 
  <teacher id="1"> 
	 <email>[email protected]</email> 
  <teacher id="2"> 
	 <name>George Kapanadze</name> 
  <teacher id="3"> 
	 <name>Tamar Kapanadze</name> 

it is correct XML ,but i have the problem:
i want to get data from this xml,i want to get attribute ,name - s,and email-s,
also subject -s
when i use the code i have written up of this page ,it does not work it does not give me any data could you change something in Main class so that it will print attribute(s) values name's values subject's values and email's values
and i must do it with method compile();
i will be glad if anyone help me.....

Is This A Good Question/Topic? 0

Replies To: Getting Data from XML file

#2 lordms12   User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: Getting Data from XML file

Posted 19 July 2008 - 12:32 PM

I did not try but it should work.

public static void main(String[] args){
	//parse the xml file and get the dom object
	//get each teacher element 

Document dom;

private void parseXmlFile(String filename){
	try {
		//get the factory
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		//Using factory get an instance of document builder
		DocumentBuilder db = dbf.newDocumentBuilder();
		//parse using builder to get DOM representation of the XML file
		dom = db.parse(filename);
	}catch(ParserConfigurationException pce) {//TODO take that line
	}catch(SAXException se) {
	}catch(IOException ioe) {

private void parseDocument(){
	//get the root elememt
	Element docEle = dom.getDocumentElement();
	//get a nodelist of <teacher> elements
	NodeList nl = docEle.getElementsByTagName("teacher");
	if(nl != null && nl.getLength() > 0) {
		for(int i = 0 ; i < nl.getLength();i++) {
			//get the employee element
			Element el = (Element)nl.item(i);

This post has been edited by lordms12: 19 July 2008 - 12:34 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1