以前,曾经解析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格式的字符串,便于表格显示
/**
* 获取孙子节点的名称
* 表头
*/
public String get_root_childName_childName()
{
String child_childname="";
StringBuffer nodename = new StringBuffer();
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("record");
int size = child.getLength();
System.out.println(size);
nodename.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
nodename.append("\n<table border=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>");
}
String values=GetNodeValue(root);
nodename.append(values);
nodename.append("\n</table>");
child_childname = nodename.toString();
}catch(Exception e){e.printStackTrace();}
return child_childname;
}
/**
* 获取孙子节点的值
* 表的内容
*/
public String GetNodeValue(Element root)
{
String nodevalue="";
StringBuffer nodevalues = new StringBuffer();
NodeList child = root.getElementsByTagName("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)
{
Element elementNode = (Element)node.getChildNodes();
NodeList elementNodeChild = elementNode.getChildNodes();
int size1 = elementNodeChild.getLength();
nodevalues.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 content = elementNode1.getTextContent();
nodevalues.append("\n <td>");
nodevalues.append(content);
nodevalues.append("</td>");
}
}
nodevalues.append("\n</tr>");
}
}
nodevalue = nodevalues.toString();
return nodevalue;
}
以上只是列举了几个xml文件的简单操作,以后会继续补充。