JAVA中調用XML
import javax.xml.parsers.*;
import org.xml.sax.*;
import java.io.*;
import org.w3c.dom.*;
public class code10_3{
static Document document;
public static void main(String[] args){
if(args.length!=1){
System.out.println("加載xml file");
return;
}
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try{
DocumentBuilder db=dbf.newDocumentBuilder();
//讀入XML文檔//saxParser.parse(new InputSource("D.xml"),myHandler);
document=db.parse(new File(args[0]));
//獲得根元素
Node root=document.getDocumentElement();
//獲得根元素的子節點列表
NodeList childs=root.getChildNodes();
GetElement(childs);
}catch(SAXException se){
//解析過程錯誤
Exception e=se;
if(se.getException()!=null)
e=se.getException();
e.printStackTrace();
}catch(ParserConfigurationException pe){
//解析器設定錯誤
pe.printStackTrace();
}catch(IOException ie){
//文件處理錯誤
ie.printStackTrace();
}
}
public static void GetElement(NodeList childs){
int i=0;
if(childs.getLength()==0)
{//該節點沒有子節點
System.out.println("該節點沒有子節點!");
}
for(i=0;i<childs.getLength();i++){
//獲取第i個子節點
Node node=childs.item(i);
//獲取節點的類型,可以是ElementNode,TextNode,DocumentNode等
short nodetype=node.getNodeType();
/*ElementNode類型的節點可包含子節點和屬性等*/
if(nodetype==Node.ELEMENT_NODE)
{
//得到節點名稱
String name=node.getNodeName();
String attrValue="",attrName="";
System.out.println("this is element! name is:"+name);
if(node.hasAttributes())
{
/*取出一個元素的屬性,得到的是一個屬性對象列表(NameNodeMap),
*在此因爲xml文檔中元素只有一個屬性,所以只取NameNodeMap中的第“0”個值。*/
Node attrNode=node.getAttributes().item(0);
/*取出該屬性節點的Name和Value,即是一個ElementNode的屬性名稱和屬性值*/
attrName=attrNode.getNodeName();
attrValue=attrNode.getNodeValue();
System.out.println("this element attr is:"+attrValue+";attrnameis:"+attrName);
}
//如有子節點,遞歸調用GetElement()
if(node.hasChildNodes())
{GetElement(node.getChildNodes());}
}
/*Text類型節點沒有子節點,節點名爲#text,節點的值爲xml文檔中元素的值*/
if(nodetype==Node.TEXT_NODE)
{
//該節點的name是"#text"
String txtName=node.getNodeName();
//取出Text類型節點的值
Node thisparent=node.getParentNode();
Node txtNode=thisparent.getChildNodes().item(0);
String txtValue=txtNode.getNodeValue();
if(txtValue.trim().length()>0)
{
System.out.println("txtName="+txtName+"; txtValue="+txtValue.trim());
}
}
}
}
}
import org.xml.sax.*;
import java.io.*;
import org.w3c.dom.*;
public class code10_3{
static Document document;
public static void main(String[] args){
if(args.length!=1){
System.out.println("加載xml file");
return;
}
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try{
DocumentBuilder db=dbf.newDocumentBuilder();
//讀入XML文檔//saxParser.parse(new InputSource("D.xml"),myHandler);
document=db.parse(new File(args[0]));
//獲得根元素
Node root=document.getDocumentElement();
//獲得根元素的子節點列表
NodeList childs=root.getChildNodes();
GetElement(childs);
}catch(SAXException se){
//解析過程錯誤
Exception e=se;
if(se.getException()!=null)
e=se.getException();
e.printStackTrace();
}catch(ParserConfigurationException pe){
//解析器設定錯誤
pe.printStackTrace();
}catch(IOException ie){
//文件處理錯誤
ie.printStackTrace();
}
}
public static void GetElement(NodeList childs){
int i=0;
if(childs.getLength()==0)
{//該節點沒有子節點
System.out.println("該節點沒有子節點!");
}
for(i=0;i<childs.getLength();i++){
//獲取第i個子節點
Node node=childs.item(i);
//獲取節點的類型,可以是ElementNode,TextNode,DocumentNode等
short nodetype=node.getNodeType();
/*ElementNode類型的節點可包含子節點和屬性等*/
if(nodetype==Node.ELEMENT_NODE)
{
//得到節點名稱
String name=node.getNodeName();
String attrValue="",attrName="";
System.out.println("this is element! name is:"+name);
if(node.hasAttributes())
{
/*取出一個元素的屬性,得到的是一個屬性對象列表(NameNodeMap),
*在此因爲xml文檔中元素只有一個屬性,所以只取NameNodeMap中的第“0”個值。*/
Node attrNode=node.getAttributes().item(0);
/*取出該屬性節點的Name和Value,即是一個ElementNode的屬性名稱和屬性值*/
attrName=attrNode.getNodeName();
attrValue=attrNode.getNodeValue();
System.out.println("this element attr is:"+attrValue+";attrnameis:"+attrName);
}
//如有子節點,遞歸調用GetElement()
if(node.hasChildNodes())
{GetElement(node.getChildNodes());}
}
/*Text類型節點沒有子節點,節點名爲#text,節點的值爲xml文檔中元素的值*/
if(nodetype==Node.TEXT_NODE)
{
//該節點的name是"#text"
String txtName=node.getNodeName();
//取出Text類型節點的值
Node thisparent=node.getParentNode();
Node txtNode=thisparent.getChildNodes().item(0);
String txtValue=txtNode.getNodeValue();
if(txtValue.trim().length()>0)
{
System.out.println("txtName="+txtName+"; txtValue="+txtValue.trim());
}
}
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.