<?xml version="1.0" encoding="gb2312"?>

<!-- RSS generated by oioj.net on 4/16/2004 ; 感谢LeXRus提供 RSS 2.0 文档; 此文件可自由使用，但请保留此行信息 --> 
<!-- Source download URL: http://blogger.org.cn/blog/rss2.asp       -->
<rss version="2.0">

<channel>
<title>honghuhuchao的博客</title>
<link>http://blogger.org.cn/blog/blog.asp?name=honghuhuchao</link>
<description>honghuhuchao的博客</description>
<copyright>blogger.org.cn</copyright>
<generator>W3CHINA Blog</generator>
<webMaster>webmaster@blogger.org.cn</webMaster>
<item>
<title><![CDATA[javascript读写xml，遇到名字空间问题]]></title>
<link>http://blogger.org.cn/blog/more.asp?name=honghuhuchao&amp;id=18115</link>
<author>honghuhuchao</author>
<pubDate>2006/9/5 19:40:21</pubDate>
<description><![CDATA[<P>var doc = new ActiveXObject("Msxml2.DOMDocument"); //ie5.5+,CreateObject("Microsoft.XMLDOM") </P>
<P>//加载文档<BR>//doc.load("b.xml");</P>
<P>//创建文件头<BR>var p = doc.createProcessingInstruction('xml','version="1.0"&nbsp; encoding="gb2312"');</P>
<P>&nbsp;&nbsp;&nbsp; //添加文件头<BR>&nbsp;&nbsp;&nbsp; doc.appendChild(p);</P>
<P>//用于直接加载时获得根接点<BR>//var root = doc.documentElement;</P>
<P>//两种方式创建根接点<BR>//&nbsp;&nbsp;&nbsp; var root = doc.createElement("students");<BR>&nbsp;&nbsp;&nbsp; var root = doc.createNode(1,"description","");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var rootAttribute1 = doc.createAttribute("xmlns");<BR>&nbsp;&nbsp;&nbsp;&nbsp; rootAttribute1.text="<A href="http://www.w3.org/2006/01/wsdl">http://www.w3.org/2006/01/wsdl</A>";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var rootAttribute2 = doc.createAttribute("targetNamespace");<BR>&nbsp;&nbsp;rootAttribute2.text="<A href="http://wr.example.com/2004/wsdl/resSvc">http://wr.example.com/2004/wsdl/resSvc</A>";<BR>&nbsp;&nbsp;var rootAttribute3 = doc.createAttribute("xmlns:tns");<BR>&nbsp;&nbsp;rootAttribute3.text="<A href="http://wr.example.com/2004/wsdl/resSvc">http://wr.example.com/2004/wsdl/resSvc</A>";<BR>&nbsp;&nbsp;var rootAttribute4 = doc.createAttribute("xmlns:ghns");<BR>&nbsp;&nbsp;rootAttribute4.text="<A href="http://wr.example.com/2004/schemas/resSvc">http://wr.example.com/2004/schemas/resSvc</A>";<BR>&nbsp;&nbsp;var rootAttribute5 = doc.createAttribute("xmlns:wsoap");<BR>&nbsp;&nbsp;rootAttribute5.text="<A href="http://www.w3.org/2006/01/wsdl/soap">http://www.w3.org/2006/01/wsdl/soap</A>";<BR>&nbsp;&nbsp;var rootAttribute6 = doc.createAttribute("xmlns:soap");<BR>&nbsp;&nbsp;rootAttribute6.text="<A href="http://www.w3.org/2003/05/soap-envelope">http://www.w3.org/2003/05/soap-envelope</A>";<BR>&nbsp;&nbsp;var rootAttribute7 = doc.createAttribute("xmlns:wsdlx");<BR>&nbsp;&nbsp;rootAttribute7.text="<A href="http://www.w3.org/2006/01/wsdl-extensions">http://www.w3.org/2006/01/wsdl-extensions</A>";<BR>&nbsp;&nbsp;root.setAttributeNode(rootAttribute1);<BR>&nbsp;&nbsp;root.setAttributeNode(rootAttribute2);<BR>&nbsp;&nbsp;root.setAttributeNode(rootAttribute3);<BR>&nbsp;&nbsp;root.setAttributeNode(rootAttribute4);<BR>&nbsp;&nbsp;root.setAttributeNode(rootAttribute5);<BR>&nbsp;&nbsp;root.setAttributeNode(rootAttribute6);<BR>&nbsp;&nbsp;root.setAttributeNode(rootAttribute7);<BR>&nbsp;&nbsp;&nbsp; //创建子接点<BR>&nbsp;&nbsp;&nbsp; var documentation = doc.createNode(1,"documentation","");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; documentation.text="This document describes the interface of component named StretchDrain";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root.appendChild(documentation);<BR>&nbsp;var&nbsp; types = doc.createNode(1,"types","");<BR>&nbsp;&nbsp; var&nbsp; schema = doc.createNode(1,"schema","xs");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var schemaAttribute1 = doc.createAttribute("xmlns:wsdlx");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; schemaAttribute1.text="<A href="http://www.w3.org/2001/XMLSchema">http://www.w3.org/2001/XMLSchema</A>";<BR>&nbsp;&nbsp;&nbsp;var schemaAttribute2 = doc.createAttribute("targetNamespace");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; schemaAttribute2.text="<A href="http://wr.example.com/2004/schemas/resSvc">http://wr.example.com/2004/schemas/resSvc</A>";<BR>&nbsp;&nbsp;&nbsp;var schemaAttribute3 = doc.createAttribute("xmlns");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; schemaAttribute3.text="<A href="http://wr.example.com/2004/schemas/resSvc">http://wr.example.com/2004/schemas/resSvc</A>";<BR>&nbsp;&nbsp;schema.setAttributeNode(schemaAttribute1);<BR>&nbsp;&nbsp;schema.setAttributeNode(schemaAttribute2);<BR>&nbsp;&nbsp;schema.setAttributeNode(schemaAttribute3);<BR>&nbsp;//var&nbsp; element = doc.createNode(1,"xs:element","");<BR>&nbsp;var docFragment = doc.createDocumentFragment();<BR>&nbsp;&nbsp; docFragment.appendChild(xmlDoc.createElement("xs:element"));<BR>&nbsp;&nbsp; root.appendChild(docFragment);<BR>&nbsp;&nbsp; types.appendChild(schema);<BR>&nbsp;&nbsp; root.appendChild(types);<BR>&nbsp;&nbsp;&nbsp; //创建孙接点<BR>&nbsp;&nbsp;&nbsp; var o = doc.createElement(parent.parent.leftFrame.document.all["input0"].value);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; o.text = "男";&nbsp;&nbsp;&nbsp; //指定其文本<BR>&nbsp; var name= doc.createElement("name");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name.text = "胡超";&nbsp;&nbsp;&nbsp; //指定其文本<BR>&nbsp;&nbsp;&nbsp; //创建属性<BR>&nbsp;&nbsp;&nbsp; var r = doc.createAttribute("id");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.value="test";</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //添加属性<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; n.setAttributeNode(r);</P>
<P>&nbsp;&nbsp;&nbsp; //创建第二个属性&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; var r1 = doc.createAttribute("class");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r1.value="tt";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //添加属性<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; n.setAttributeNode(r1);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //删除第二个属性<BR>&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp; n.removeAttribute("class");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //添加孙接点<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; n.appendChild(o);<BR>&nbsp;&nbsp; n.appendChild(name);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(n.xml);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //添加文本接点<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // n.appendChild(doc.createTextNode("this is a text node."));</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //添加注释<BR>&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp; n.appendChild(doc.createComment("this is a comment\n"));<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //添加子接点<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root.appendChild(n);<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; //复制接点<BR>&nbsp; //&nbsp; var m = n.cloneNode(true);</P>
<P>&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; root.appendChild(m);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(root.xlm)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //删除接点<BR>&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp; root.removeChild(root.childNodes(0));</P>
<P>&nbsp;&nbsp;&nbsp; //创建数据段<BR>&nbsp;&nbsp; // var c = doc.createCDATASection("this is a cdata");<BR>&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; c.text = "hi,cdata";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //添加数据段<BR>&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root.appendChild(c);<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; //添加根接点<BR>&nbsp;&nbsp;&nbsp; doc.appendChild(root);</P>
<P>&nbsp;&nbsp;&nbsp; //查找接点<BR>&nbsp;&nbsp;&nbsp; var a = doc.getElementsByTagName("ttyp");<BR>&nbsp;&nbsp;&nbsp; //var a = doc.selectNodes("//ttyp");</P>
<P>&nbsp;&nbsp;&nbsp; //显示改接点的属性<BR>&nbsp;&nbsp;&nbsp; for(var i= 0;i&lt;a.length;i++)<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(a[i].xml);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(var j=0;j&lt;a[i].attributes.length;j++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(a[i].attributes[j].name);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; //XML保存（需要在服务端，客户端用FSO）<BR>&nbsp;&nbsp;&nbsp; //doc.save();<BR>&nbsp;&nbsp;&nbsp;&nbsp; var fso, filehandle;<BR>&nbsp;&nbsp; fso = new ActiveXObject("Scripting.FileSystemObject");<BR>&nbsp; filehandle = fso.CreateTextFile("test1.xml", true);<BR>&nbsp;&nbsp; filehandle.Write(doc.xml);<BR>&nbsp; filehandle.Close();<BR>&nbsp;&nbsp; alert(doc.xml);<BR>&nbsp; //root.AppendChild(rootElement)<BR>&nbsp;&nbsp;&nbsp; //查看根接点XML<BR>&nbsp;&nbsp;&nbsp; if(n)<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(n.ownerDocument.xml);<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>//--&gt;</P>]]></description>
</item>
</channel>
</rss>