Getting Data from XML file

Getting Data from XML file

Page 1 of 1

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

#1 Guest_dato.java*


Reputation:

Getting Data from XML file

Post icon  Posted 25 June 2008 - 11:11 AM

here is code:
import java.io.IOException;
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++) {
		System.out.println(nodes.item(i).getNodeValue()); 
	}
*/
  }

}



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="http://www.tsu.ge/oop3/homework" 
			   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
					  xsi:schemaLocation="http://www.tsu.ge/oop3/homework D:\XML\teachers.xsd"> 
  <teacher id="1"> 
	 <name>m</name> 
	 <email>mikheil@mikheil.com</email> 
	 <subject>oop</subject> 
  </teacher> 
  <teacher id="2"> 
	 <name>George Kapanadze</name> 
	 <subject>history</subject> 
  </teacher> 
  <teacher id="3"> 
	 <name>Tamar Kapanadze</name> 
	 <subject>biology</subject> 
  </teacher> 
</teachers> 




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  Icon 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
	parseXmlFile(filename);
	
	//get each teacher element 
	parseDocument();
}

Document dom;

private void parseXmlFile(String filename){
	try {
		//get the factory
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	
		dbf.setNamespaceAware(true);
		dbf.setValidating(true);
		
		//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
		pce.printStackTrace();
	}catch(SAXException se) {
		se.printStackTrace();
	}catch(IOException ioe) {
		ioe.printStackTrace();
	}
}

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);
			
			el.getAttribute("name");
			el.getAttribute("email");
			el.getAttribute("subject");
		}
	}
}

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1