1)Xml文档示例(xmlsample.xml):
Code
<?xml version="1.0" encoding="iso-8859-1" ?>
<music>
<song title="Oh,girl">
<artist>The Chi-lites</artist>
<genre>Soul</genre>
<album>A lonely man</album>
<year>1972</year>
</song>
<song title="What if">
<artist>Babyface</artist>
<genre>R&B</genre>
<album>unknown</album>
<year></year>
</song>
<song title="How come,how long">
<artist>Babyface</artist>
<genre>R&B</genre>
<album>The essential babyface</album>
& ......
1)Xml文档示例(xmlsample.xml):
Code
<?xml version="1.0" encoding="iso-8859-1" ?>
<music>
<song title="Oh,girl">
<artist>The Chi-lites</artist>
<genre>Soul</genre>
<album>A lonely man</album>
<year>1972</year>
</song>
<song title="What if">
<artist>Babyface</artist>
<genre>R&B</genre>
<album>unknown</album>
<year></year>
</song>
<song title="How come,how long">
<artist>Babyface</artist>
<genre>R&B</genre>
<album>The essential babyface</album>
& ......
2)如何创建一个xml文档
由于xml实质也只是一个文本文件,所以最简单你可以直接使用System.IO下的类生成一个文件,并存储成xml文件,当然,你需要手动保证该文件形式良好,比如必须有根元素、必须有关闭标签、必须正确嵌套等等。
如果你懒得自己去想文件的形式,可以使用System.Xml下的类。
Code
XmlDocument xdoc = new XmlDocument();
XmlDeclaration xdcl = xdoc.CreateXmlDeclaration("1.0", "iso-8859-1", "");
xdoc.AppendChild(xdcl);
//XmlNode xnode = xdoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
//xdoc.AppendChild(xnode);
//Of course you can use CreateNode here(with the XmlNodeType.Element),
//so no more questions about 'what difference between this 2 methods?',
XmlElement root = xdoc.CreateElement("music");
xdoc.AppendChild(root);
XmlElement xelm = xdoc.CreateElement("song");
xelm.SetAttribute("title", "Oh,girl");
xelm.InnerText = "Oh,girl,i'd be in trouble if you left me now";
root.AppendChild(xelm);
try
{
xdoc.Save("xmlsample-1.xml");
}
c ......
2)如何创建一个xml文档
由于xml实质也只是一个文本文件,所以最简单你可以直接使用System.IO下的类生成一个文件,并存储成xml文件,当然,你需要手动保证该文件形式良好,比如必须有根元素、必须有关闭标签、必须正确嵌套等等。
如果你懒得自己去想文件的形式,可以使用System.Xml下的类。
Code
XmlDocument xdoc = new XmlDocument();
XmlDeclaration xdcl = xdoc.CreateXmlDeclaration("1.0", "iso-8859-1", "");
xdoc.AppendChild(xdcl);
//XmlNode xnode = xdoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
//xdoc.AppendChild(xnode);
//Of course you can use CreateNode here(with the XmlNodeType.Element),
//so no more questions about 'what difference between this 2 methods?',
XmlElement root = xdoc.CreateElement("music");
xdoc.AppendChild(root);
XmlElement xelm = xdoc.CreateElement("song");
xelm.SetAttribute("title", "Oh,girl");
xelm.InnerText = "Oh,girl,i'd be in trouble if you left me now";
root.AppendChild(xelm);
try
{
xdoc.Save("xmlsample-1.xml");
}
c ......
3)读取、查找
当你手头有一个xml文件后,可以使用XmlDocument.Load()方法将其加载进来以便处理,所以“读取”没有什么可说的。而“查找”操作往往涉及XPath,这里只是我认为的比较常用到的查找操作,XPath这玩意实在是很强大很暴力。
回头看示例文档xmlsample.xml,我们可能遇到这样的需求:
(A)查找歌曲“Hurt”的演唱者?
(B)查找流派为“R&B”的歌曲名称?
(C)查找发行年份在2004以前的歌曲的演唱者?
(D)倒数第二首歌的歌曲名称?
针对上述问题,你当然可以通过递归遍历各结点来查找,不过使用XPath将是更为高效的方案:
问题(A)
Code
XmlDocument doc = new XmlDocument();
doc.Load("xmlsample.xml");
//
XmlNodeList xnl = doc.SelectNodes("/music/song[@title='Hurt']/artist");
foreach (XmlNode n in xnl)
{
Console.WriteLine(n.InnerText);
}
(以“/”起始的路径必定是绝对路径,即从根元素起。而对属性的引用前面要加“@”)
问题(B)
Code
XmlDocument doc = new XmlDocument ......
3)读取、查找
当你手头有一个xml文件后,可以使用XmlDocument.Load()方法将其加载进来以便处理,所以“读取”没有什么可说的。而“查找”操作往往涉及XPath,这里只是我认为的比较常用到的查找操作,XPath这玩意实在是很强大很暴力。
回头看示例文档xmlsample.xml,我们可能遇到这样的需求:
(A)查找歌曲“Hurt”的演唱者?
(B)查找流派为“R&B”的歌曲名称?
(C)查找发行年份在2004以前的歌曲的演唱者?
(D)倒数第二首歌的歌曲名称?
针对上述问题,你当然可以通过递归遍历各结点来查找,不过使用XPath将是更为高效的方案:
问题(A)
Code
XmlDocument doc = new XmlDocument();
doc.Load("xmlsample.xml");
//
XmlNodeList xnl = doc.SelectNodes("/music/song[@title='Hurt']/artist");
foreach (XmlNode n in xnl)
{
Console.WriteLine(n.InnerText);
}
(以“/”起始的路径必定是绝对路径,即从根元素起。而对属性的引用前面要加“@”)
问题(B)
Code
XmlDocument doc = new XmlDocument ......
public class yzzSerialize
{
private yzzSerialize()
{ }
private static yzzCache cache = new yzzCache();
public static T GetfromXml<T>(string xmlpath, T t)
{
using (FileStream fs = new FileStream(xmlpath, FileMode.Open, FileAccess.Read))
{
Type type = typeof(T);
XmlSerializer xs = cache[type.FullName] as XmlSerializer;
if (xs == null)
{
xs = new XmlSerializer(type);
cache[type.FullName] = xs;
}
t = (T)xs.Deserialize(fs);
fs.Close();
return t;
}
}
public static void SetToXml<T>(string xmlpath, T t)
{
if (t == null)
return;
using (FileStream fs = new FileStream(xmlpath, FileMode.Create, FileAccess.Write))
{
Type t ......
public class yzzSerialize
{
private yzzSerialize()
{ }
private static yzzCache cache = new yzzCache();
public static T GetfromXml<T>(string xmlpath, T t)
{
using (FileStream fs = new FileStream(xmlpath, FileMode.Open, FileAccess.Read))
{
Type type = typeof(T);
XmlSerializer xs = cache[type.FullName] as XmlSerializer;
if (xs == null)
{
xs = new XmlSerializer(type);
cache[type.FullName] = xs;
}
t = (T)xs.Deserialize(fs);
fs.Close();
return t;
}
}
public static void SetToXml<T>(string xmlpath, T t)
{
if (t == null)
return;
using (FileStream fs = new FileStream(xmlpath, FileMode.Create, FileAccess.Write))
{
Type t ......
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
SAXReader reader = new SAXReader();
try {
Document doc = reader.read("ParserSQL.xml");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Element sql = doc.getRootElement();
List list = sql.selectNodes("节点的名字");
for (int i = 0; i < list.size(); i++) {
Element node1 = (Element) list.get(i);
System.out.println(node1);
List list1 = node1.elements();
......
1、安装DOM4j
http://www.dom4j.org/
2、安装jaxen
http://jaxen.org/releases.html
3、代码
package extract;
import java.io.*;
import org.dom4j.*;
import org.dom4j.io.*;
import java.util.*;
public class XmlExtract {
private SAXReader reader;
private Document document;
/**
* @param args
*/
public XmlExtract(){
reader=new SAXReader();
try {
document=reader.read(new File("./htmls/abcde.xml"));
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void exxml(String path){
if (document==null) return;
List l=document.selectNode ......