see following class:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
*
* @author prasobh.k
*/
public class ParseMYXML {
private Document doc = null;
public ParseMYXML() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse("c://myxml.xml");
doc.getDocumentElement().normalize();
printCompanyNames();
printAttributeOfRootTag();
} catch (Exception e) {
System.out.println("exc " + e);
}
}
public static void main(String[] args) {
new ParseMYXML();
}
public void printCompanyNames() {
NodeList lastNameList = doc.getElementsByTagName("companyname");
Element fstNmElmnt = (Element) lastNameList.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
System.out.println(((Node) fstNm.item(0)).getNodeValue());
}
public void printAttributeOfRootTag() {
Element link = null;
try {
NodeList links = doc.getElementsByTagName("company");
link = (Element) links.item(0);
} catch (Exception e) {
}
String date = link.getAttribute("date");
System.out.println("date :" + date);
}
}
<?xml version="1.0"?>
<company date="8/4/2011">
<staff >
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff>
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff >
<companyname>MyCompany</companyname>
</company>
MyCompany
date :8/4/2011
ParseMYXML.java
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
*
* @author prasobh.k
*/
public class ParseMYXML {
private Document doc = null;
public ParseMYXML() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse("c://myxml.xml");
doc.getDocumentElement().normalize();
printCompanyNames();
printAttributeOfRootTag();
} catch (Exception e) {
System.out.println("exc " + e);
}
}
public static void main(String[] args) {
new ParseMYXML();
}
public void printCompanyNames() {
NodeList lastNameList = doc.getElementsByTagName("companyname");
Element fstNmElmnt = (Element) lastNameList.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
System.out.println(((Node) fstNm.item(0)).getNodeValue());
}
public void printAttributeOfRootTag() {
Element link = null;
try {
NodeList links = doc.getElementsByTagName("company");
link = (Element) links.item(0);
} catch (Exception e) {
}
String date = link.getAttribute("date");
System.out.println("date :" + date);
}
}
myxml.xml
<?xml version="1.0"?>
<company date="8/4/2011">
<staff >
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff>
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff >
<companyname>MyCompany</companyname>
</company>
output:
MyCompany
date :8/4/2011
No comments:
Post a Comment