易截截图软件、单文件、免安装、纯绿色、仅160KB
热门标签: c c# c++ asp asp.net linux php jsp java vb Python Ruby mysql sql access Sqlite sqlserver delphi javascript Oracle ajax wap mssql html css flash flex dreamweaver xml
 最新文章 : xml

C#小Tip:Xml操作简明手册 1


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&amp;B</genre>
    <album>unknown</album>
    <year></year>
  </song>
  <song title="How come,how long">
    <artist>Babyface</artist>
    <genre>R&amp;B</genre>
    <album>The essential babyface</album>
 & ......

C#小Tip:Xml操作简明手册 1


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&amp;B</genre>
    <album>unknown</album>
    <year></year>
  </song>
  <song title="How come,how long">
    <artist>Babyface</artist>
    <genre>R&amp;B</genre>
    <album>The essential babyface</album>
 & ......

C#小Tip:Xml操作简明手册 2


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 ......

C#小Tip:Xml操作简明手册 2


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 ......

C#小Tip:Xml操作简明手册 3

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 ......

C#小Tip:Xml操作简明手册 3

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 ......

C#序列化与反序列化Xml,利用范型做通用化处理

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 ......

C#序列化与反序列化Xml,利用范型做通用化处理

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 ......

dom4j 解析 xml 例子

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();
   ......

lucene 解析xml 深未来技术

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 ......
总记录数:815; 总页数:136; 每页6 条; 首页 上一页 [113] [114] [115] [116] 117 [118] [119] [120] [121] [122]  下一页 尾页
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号