您的位置:首页 > Web前端 > Node.js

20100603 学习记录:常用类型--XMLDocument XMLNode XMLElement XMLAttribute XMLFragment XML

2010-06-03 10:58 597 查看
<?xml version="1.0"?>

<book level="1">

<Name>c++</Name>

<Price>20</Price>

<info><k>1</k></info>

<info><k>2</k> </info>

</book>

2.1.4 text

Represents the text content of the node or the concatenated text representing the node and its descendants. Read/write

该点及下面派生的所有点的全部节点值,可读可写

<price>20</price>则text为20"Name"节点的text为"c++"

2.1.6 nodeName

Returns the qualified name for attribute, document type, element, entity, or notation nodes. Returns a fixed string for all

other node types. Read-only

该节点名称"Name"节点的nodeName为"Name","book"节点的nodeName为"book"

2.1.7 documentElement

Contains the root element of the document

xml的根节点上面的xml的根节点为"book"

2.2.1 loadXML

Loads an XML document using the supplied string

2.2.2 load

Loads an XML document from the specified locati

参数的路径为服务器端的,是相对路径

2.2.3 selectSingleNode

Applies the specified pattern-matching operation to this node's context and returns the first matching node返回第一个匹配的项

2.2.4 selectNodes

Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes as IXMLDOMNodeList

符合条件的所有项。

2.2.5 getElementsByTagName

Returns a collection of elements that have the specified name

返回与元素名匹配的一个node的集合

2.2.6 hasChildNodes

Provides a fast way to determine whether a node has children

判断是否含有子节点返回值为bool值

节点定义的说明:无论是xmldocument,xmlelement,xmlattribute 以及 xmlfragment 均为xmlnode类的子类;在dom中,每一个xmlnode并非只是单单的一对标签,而是包含了该对标签内所有内容;例如:

-

<booklist>

<book genre="novel" ISBN="1-861001-57-5">

<title>Pride And Prejudice</title>

<</book>

<</booklist>

常用节点类型类的含义:

在上面的xml代码段内,booklist节点代表的是从 <booklist> 向下到 </booklist>内的所有内容,大家千万不要把定义搞混淆。

下面我来介绍一下这几个类的各代表什么含义:

xmldocument 代表:类型为document的节点,注意:此类与其他类不同,该类有一个属性为根节点(documentelement)且该根节点为document的子节点;所以『 XmlElement xf = (xmlelement)xd.FirstChild ; 与 XmlElement xn = xd.DocumentElement; 等效』对于xmldocument内的任何节点的增删改节点操作必须在document节点类上创建,并在document下任何一个子节点上增删改;xmldocuemnt节点的默认名称为:“#document”,而其他节点类型的名称为:tagname;

xmlelement 代表:类型为element的节点,可能有一个父节点以及多个子节点,当element作为根节点时,无父节点。

xmlattribute 代表:类型为attribute的节点,属性节点无父子关系

xmlfragment 代表:类型为fragment的节点,这里说明一下,fragment为xml碎片的含义,其实就是一个或者几个节点段。此类无根节点这样的属性,只要fragment内的xml文本符合xml规范,就ok。

XML常用方法的使用:

1,新建xmldocument对象,导入xml文件或xml string;

a,导入xml文件:

xmldocument xd = new xmldocument;

xd.load(“d://1.xml”);

b,导入xml string;

xmldocument xd = new xmldocument;

xd.loadxml("<booklist><book genre='novel' ISBN='1-861001-57-5'>" +

"<title>Pride And Prejudice</title>" +

"</book></booklist>");

//或者

xd.innerxml("<booklist><book genre='novel' ISBN='1-861001-57-5'>" +

"<title>Pride And Prejudice</title>" +

"</book></booklist>");

2,插入xml节点:

XmlDocument xd = new XmlDocument();

xd.LoadXml("<booklist><book genre='novel' ISBN='1-861001-57-5'>" +

"<title>Pride And Prejudice</title>" +

"</book></booklist>");

XmlElement xf = (xmlelement)xd.FirstChild; //等效于 XmlElement xn = xd.DocumentElement;

XmlNode book = xf.FirstChild;

//为document节点创建一个新的节点;

XmlElement xe = xd.CreateElement("jiangyang");

xe.SetAttribute("name", "jiangyang");

xe.InnerText="number one";

//获得根节点的第一个子节点;

XmlNode title = xn.FirstChild;

title.PrependChild(xe); //在title的第一个子节点前插入xe节点,而 title.apendChild(xe);为在第一个子节点后插入xe节点;

结果:

<booklist>

<book genre="novel" ISBN="1-861001-57-5">

<jiangyang name="jiangyang">number one</jiangyang>

<title>Pride And Prejudice</title>

</book>

</booklist>

[b]3,插入另一个根节点:[/b]

XmlDocument xd = new XmlDocument();

xd.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><booklist xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><book genre='novel' ISBN='1-861001-57-5'>" +"<title>Pride And Prejudice</title>" +"</book></booklist>");

xd.InnerXml = xd.InnerXml.Replace("book","booklist");//将标签为book的替换为booklist;

xd.InnerXml = xd.InnerXml.Substring(0,xd.InnerXml.IndexOf("<list"))+"<add>" + xd.InnerXml.Substring(xd.InnerXml.IndexOf("<list")) + "</add>"; //在现有根节点上加入根节点。

4. 节点内的几个属性与方法介绍:

a,(属性)innertext(当前节点内包含的所有text节点,包含当前节点的子text节点)

b,(属性)innerxml (当前节点内包含的所有节点,不包含当前节点)

c,(属性)prefix (当前节点的前缀)

d,(方法)appendchild (在当前节点的最后一个子节点位置插入节点)

e,(方法)prependchid (在当前节点的第一个子节点位置插入节点)

f,(方法)getelementsbytag(根据tagname获得所有的elements,并将其保存在nodelists内)

g,(方法)getelementbyid (根据id获得一个element,返回类型为xmlNode类型)

h,(方法)selectNodes (string xpath) xpath 具体内容请看本博客内的关于xpath详细介绍的几篇文章。

I,(方法)Create系列,仅仅document节点对象含有。

J,(方法)Save,(多个重载,将xmldocument保存到文件内、writer内、stream内)

K, (方法) xd.InnerXml.Insert方法

例程:

XmlDocument xd = new XmlDocument();

xd.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><booklist xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><book genre='novel' ISBN='1-861001-57-5'>" +

"<title>Pride And Prejudice</title>" +

"</book></booklist>");

xd.InnerXml = xd.InnerXml.Insert(xd.InnerXml.IndexOf("<book"), "<name></name>");

结果:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>

<booklist xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">

<book genre='novel' ISBN='1-861001-57-5'>

<name></name>

<title>Pride And Prejudice</title>

</book></booklist>

[b]5,将XmlDocumentFragment插入到xmldocument内:[/b]

XmlDocumentFragment xdf = xd.CreateDocumentFragment();

xdf.InnerXml = "<book genre='novel' ISBN='1-861001-57-5'>" +

"<title>Pride And Prejudice</title>" +

"</book>";

xn.AppendChild(xdf);

答疑:为什么要把xmldocument作为节点类型呢?为什么不把文档节点直接定义为根节点呢?

这个问题留个自己以后思考,现在真是不清楚呀。

下面给出一段代码:

XmlDocument xd = new XmlDocument();

xd.LoadXml("<booklist><book genre='novel' ISBN='1-861001-57-5'>" +

"<title>Pride And Prejudice</title>" +

"</book></booklist>");

//新建一个元素节点,定义他的attribute节点与text节点

XmlElement xe = xd.CreateElement("jiangyang");

xe.SetAttribute("name", "jiangyang");

xe.InnerText="number one";

//将该节点插入到xmldocuement内;

//xd.documentElement.AppendChild(xe);

xd.AppendChild(xe);

上面的代码在执行到xd.AppendChild(xe)时出现错误,提示:已经存在根节点。

替换为:xd.documentElement.AppendChild(xe);就ok了。

注意,这里加上documentElement之后,实际是xe仍然是被加在根节点下,不可能作为根节点的兄弟添加!
http://hi.baidu.com/jiangyangw3r/blog/item/c05441a9163833f41e17a25d.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: