使用DOM4J和xpath解析XML(一)
books.xml:
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<books>
<!--This is a test for dom4j, jakoes, 2007.7.19-->
<book show="yes" url="lucene.net">
<title id="456">Lucene Studing</title>
</book>
<book show="yes" url="dom4j.com">
<title id="123">Dom4j Tutorials</title>
</book>
<book show="no" url="spring.org">
<title id="789">Spring in Action</title>
</book>
<owner>O'Reilly</owner>
</books>
<?xml version="1.0" encoding="UTF-8"?>
<books>
<!--This is a test for dom4j, jakoes, 2007.7.19-->
<book show="yes" url="lucene.net">
<title id="456">Lucene Studing</title>
</book>
<book show="yes" url="dom4j.com">
<title id="123">Dom4j Tutorials</title>
</book>
<book show="no" url="spring.org">
<title id="789">Spring in Action</title>
</book>
<owner>O'Reilly</owner>
</books>
下面我们使用dom4j的xPath来解析:
Java代码
public void parseBooks(){
SAXReader reader = new SAXReader();  
相关文档:
第一种方式--使用MSXML2.DOMDOCUMENT.4.0
创建xmldoc对象,并装载一个xml
Set xmldoc = CreateObject("MSXML2.DOMDOCUMENT.4.0")
xmldoc.loadXML ("一个具体的XML实体")
选择需要筛选的结点
Set nodeList = xmldoc.selectNodes("//*[count(ancestor::*)= 2]") '根节点的第二代后代
Set nodeList = xmldoc ......
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.NodeList; ......
protected void Page_Load(object sender, EventArgs e)
{
lblValue.Text = string.Format("{0:N}", 2500000);
lblValue.Text = string.Format("{0:C3}", 2);
......
// TestXml.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include "tinyxml.h"
#include "lang.h"
#include <string>
using namespace std;
using namespace std;
#pragma comment(lib, "tinyxmld.lib")
B ......
private XmlDocument xmlDoc;
private void btnCreateXml_Click(object sender, EventArgs e)
{
//自 ......