转载,原文地址找不到了!
以前,曾经解析xml文件,仅仅为了能够实现结果而解析,觉得简单。在昨天做的项目中,需要解析xml文件,发现之前并没有深入进去。静下心来,好好的看了看书,受益匪浅。下面举几个常见的例子说明一下Dom的基本用法。
引用的xml文件(borrowed.xml):
〈?xml version="1.0" encoding="GB2312"?>
〈NewDataSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="C:\sourcelist\borrowed.xsd">
〈record>
〈BDate>2001-06-1800:00:00.0〈/BDate>
〈BPieces>0〈/BPieces>
〈DNo>00020〈/DNo>
〈MNo>0001〈/MNo>
〈RDate>2001-09-1800:00:00.0〈/RDate>
〈RNo>01 1〈/RNo>
〈STATUS>1〈/STATUS>
〈/record>
〈record>
〈BDate>2001-06-1800:00:00.0〈/BDate>
〈BPieces>0〈/BPieces>
〈DNo>00053〈/DNo>
〈MNo>0001〈/MNo>
〈RDate>2001-09-1800:00:00.0〈/RDate>
〈RNo>01 2〈/RNo>
〈STATUS>1〈/STATUS>
〈/record>
〈record>
〈BDate>2001-06-1800:00:00.0〈/BDate>
〈BPieces>0〈/BPieces>
〈DNo>00086〈/DNo>
〈MNo>0004〈/MNo>
〈RDate>2001-09-1800:00:00.0〈/RDate>
〈RNo>01 3〈/RNo>
〈STATUS>1〈/STATUS>
〈/record>
〈record>
〈BDate>2001-06-1800:00:00.0〈/BDate>
〈BPieces>0〈/BPieces>
〈DNo>00053〈/DNo>
〈MNo>0001〈/MNo>
〈RDate>2001-09-1800:00:00.0〈/RDate>
〈RNo>01 2〈/RNo>
〈STATUS>1〈/STATUS>
〈/record>
〈/NewDataSet>
1. 获取xml文件的版本和字符格式
public void get_versionANDencoding()
{
try
{
DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
DocumentBuilder builder =factory.newDocumentBuilder();
Document document =builder.parse(new File("borrowed.xml"));
String version =document.getXmlVersion();
System.out.println("xml文件的版本为:"+version);
String encoding =document.getXmlEncoding();
System.out.println("xml文件的编码为:"+encoding);
}catch (Exceptione){e.printStackTrace();}
}
2. 获取xml文件的头结点
public void get_rootName()
{
try
{
DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
DocumentBuilder builder =factory.newDocumentBuilder();
Document document =builder.parse(new File("borrowed.xml"));
Element root =document.getDocumentElement();
String rootName =root.getNodeName();
System.out.println("xml文件根节点的名称为:"+ rootName);
}catch(Exception e){e.printStackTrace();}
}
3. 获取xml文件的头结点的子节点以及子节点的相关内容
方法一:
public void get_root_childNameAndcontent()
{
try
{
DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
DocumentBuilder builder =factory.newDocumentBuilder();
Document document =builder.parse(new File("borrowed.xml"));
NodeList nodelist =document.getElementsByTagName_r("record");
int size =nodelist.getLength();
for(int i=0; i〈size;i++)
{
Node node =nodelist.item(i);
String name= node.getNodeName();
Stringcontent = node.getTextContent();
System.out.println(name);
System.out.println(" " + content);
}
}catch(Exception e){e.printStackTrace();}
}
方法二:
public void get_root_childNameAndContent()
{
try
{
DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
DocumentBuilder builder =factory.newDocumentBuilder();
Document document =builder.parse(new File("borrowed.xml"));
Element root =document.getDocumentElement();
NodeList nodelist =root.getChildNodes();
int size =nodelist.getLength();
System.out.println(size);
for(int i=0; i〈size;i++)
{
Node node =nodelist.item(i);
if(node.getNodeType()== Node.ELEMENT_NODE)
{
Element elementNode = (Element)node;
String name = elementNode.getNodeName();
String content = elementNode.getTextContent();
System.out.println(name + "\n" + content);
}
}
}catch(Exception e){e.printStackTrace();}
}
4. 获取xml文件头结点的子节点名称
public void get_root_childName()
{
try
{
DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
DocumentBuilder builder =factory.newDocumentBuilder();
Document document =builder.parse(new File("borrowed.xml"));
Element root =document.getDocumentElement();
NodeList nodelist =root.getChildNodes();
int size =nodelist.getLength();
System.out.println(size);
for(int i=0; i〈size;i++)
{
Node node =nodelist.item(i);
if(node.getNodeType()== Node.ELEMENT_NODE)
{
Element elementNode = (Element)node;
String name = elementNode.getNodeName();
System.out.println(name);
}
}
}catch(Exception e){e.printStackTrace();}
}
5. 获取孙子节点的名称和值,生成xml格式的字符串,便于表格显示
public Stringget_root_childName_childName()
{
String child_childname="";
StringBuffer nodename = newStringBuffer();
try
{
DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
DocumentBuilder builder =factory.newDocumentBuilder();
Document document =builder.parse(new File("borrowed.xml"));
Element root =document.getDocumentElement();
NodeList child =root.getElementsByTagName_r("record");
int size =child.getLength();
System.out.println(size);
nodename.append("〈?xmlversion="1.0" encoding="GB2312"?>");
nodename.append("\n〈tableborder=1>");
Node node =child.item(1);
if (node.getNodeType()==Node.ELEMENT_NODE)
{
Element elementNode = (Element)node.getChildNodes();
NodeList elementNodeChild = elementNode.getChildNodes();
int size1 = elementNodeChild.getLength();
nodename.append("\n〈tr>");
for (int j=0; j〈size1;j++)
{
Node node1 = elementNodeChild.item(j);
if (node1.getNodeType()==Node.ELEMENT_NODE)
{
Element elementNode1 = (Element)node1.getChildNodes();
String name = elementNode1.getNodeName();
nodename.append("\n 〈th>");
nodename.append(name);
nodename.append("〈/th>");
}
}
nodename.append("\n〈/tr>");
}
Stringvalues=GetNodue(root);
nodename.append(values);
nodename.append("\n〈/table>");
child_childname =nodename.toString();
}catch(Exception e){e.printStackTrace();}
return child_childname;
}
public String GetNodue(Element root)
{
String nodue="";
StringBuffer nodues = newStringBuffer();
NodeList child =root.getElementsByTagName_r("record");
int size = child.getLength();
System.out.println(size);
for(int i=0; i〈size; i++)
{
Node node =child.item(i);
if (node.getNodeType()==Node.ELEMENT_NODE)
{
ElementelementNode = (Element)node.getChildNodes();
NodeListelementNodeChild = elementNode.getChildNodes();
int size1 =elementNodeChild.getLength();
nodues.append("\n〈tr>");
for (intj=0; j〈size1;j++)
{
Node node1 = elementNodeChild.item(j);
if (node1.getNodeType()==Node.ELEMENT_NODE)
{
Element elementNode1 = (Element)node1.getChildNodes();
String content = elementNode1.getTextContent();
nodues.append("\n 〈td>");
nodues.append(content);
nodues.append("〈/td>");
}
}
nodues.append("\n〈/tr>");
}
}
nodue = nodues.toString();
return nodue;
}
以上只是列举了几个xml文件的简单操作