以前,曾经解析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-18 00:00:00.0〈/BDate>
〈BPieces>0〈/BPieces>
〈DNo>00020〈/DNo>
〈MNo>0001〈/MNo>
〈RDate>2001-09-18 00:00:00.0〈/RDate>
〈RNo>01 1〈/RNo>
〈STATUS>1〈/STATUS>
〈/record>
〈record>
〈BDate>2001-06-18 00:00:00.0〈/BDate>
〈BPieces>0〈/BPieces>
〈DNo>00053〈/DNo>
〈MNo>0001〈/MNo>
〈RDate>2001-09-18 00:00:00.0〈/RDate>
〈RNo>01 2〈/RNo>
〈STATUS>1〈/STATUS>
〈/record>
〈record>
〈BDate>2001-06-18 00:00:00.0〈/BDate>
〈BPieces>0〈/BPieces>
〈DNo>00086〈/DNo>
〈MNo>0004〈/MNo>
〈RDate>2001-09-18 00:00:00.0〈/RDate>
〈RNo>01 3〈/RNo>
〈STATUS>1〈/STATUS>
〈/record>
〈record>
〈BDate>2001-06-18 00:00:00.0〈/BDate>
〈BPieces>0〈/BPieces>
〈DNo>00053〈/DNo>
〈MNo>0001〈/MNo>
〈RDate>2001-09-18 00:00:00.0〈/RDate>
〈RNo>01 2〈/RNo>
〈STATUS>1〈/STATUS>
〈/record>
〈/NewDataSet>
1. 获取xml文件的版本和字符格式
/**
* 获取xml文件的版本和字符格式
* document.getXmlVersion()
* document.getXmlEncoding()
*/
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 (Exception e){e.printStackTrace();}
}
2. 获取xml文件的头结点
/**
* 获取xml文件的头结点
* document.getDocumentElement().getNodeName()
*/
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文件的头结点的子节点以及子节点的相关内容
方法一:
/**
* 获取xml文件的头结点的子节点以及子节点的相关内容
* document.getElementsByTagName("record")
* NodeList
* item()
*/
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("record");
int size = nodelist.getLength();
for(int i=0; i〈size; i++)
{
Node node = nodelist.item(i);
String name = node.getNodeName();
String content = node.getTextContent();
System.out.println(name);
System.out.println(" " + content);
}
}catch(Exception e){e.printStackTrace();}
}
方法二:
/**
* 获取xml文件头结点的子节点以及子节点的相关内容
* NodeList nodelist = root.getChildNodes();
* Element elementNode = (Element)node
*/
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文件头结点的子节点名称
/**
* 获取xml文件头结点的子节点名称
* node.getNodeType()== Node.ELEMENT_NODE
* Element elementNode = (Element)node
*/
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格式的字符串,便