好多人问怎么操作xml,这是我常用的操作xml文档的方法,也请高手补充

nittystone 2003-02-16 12:24:23
Dom操作xml的常用方法
(Dom的全部方法和属性可到vs的对象浏览器察看)

'创建DOM对象
set objDom=server.CreateObject("microsoft.xmldom")

'取得xml数据
'方法1 取得xml文件的xml数据
objDom.load("c:\test.xml")
'方法2 取得xml数据串的数据
objDom.loadxml("<people><man name="sd"></people>")

'创建一个节点对象
Set Newnode=objDom.CreateElement("people")
'给这个节点赴值
Newnode.Text="人"
' 给这个节点添加属性
Set NewAttribute=objDom.CreateNode("attribute","name","")
NewAttribute.Text= "张三"
Newnode.SetAttributeNode NewAttribute
'给这个节点添加子节点
Set NewnodeChild=objDom.CreateElement("address")
Newnode.appendChild NewnodeChild
'保存这个节点对象
objDom.appendChild Newnode
objDom.save("c:\test.xml")

'查找一个节点对象
set objtofind=objdom.documentElement.SelectSingleNode("//people/man")
'取出这个节点对象的 节点名,节点值,某个属性值,和全部的xml
nodename=objtofind.nodename
nodevalue=objtofind.text
objtofind.GetAttributeNode("name").NodeValue '属性名为name的属性值

'取出一个属性节点对象
set objattrtofind=objdom.documentElement.SelectSingleNode("//people/man"). GetAttributeNode("name")
'取出这个节点的属性名,属性值
nodeattrname=objattrtofind.nodename
nodeattrvalue=objattrtofind.nodevalue

'删除一个节点对象
set objnode=objdom.documentElement.SelectSingleNode("//people/man") '要删除的节点
set objparentnode=objdom.documentElement.SelectSingleNode("//people") '要删除的节点的父节点
objparentnode.removeChild objnode

'取出一个节点的字节点集合
set objnodes=objdom.documentElement.SelectSingleNode("//people/man").ChildNodes
遍历这个集合
方法1
for each element in objnodes
response.write element.nodename 字节点名
response.write element.text 字节点值
next
方法2
domlength=objnodes.length
for i = 0 to domlength-1
response.write objnodes.childnodes(i).nodename 字节点名
response.write objnodes.childnodes(i).text 字节点值
next

'取出一个节点的属性集合
set objnodes=objdom.documentElement.SelectSingleNode("//people/man").GetAttributeNode("name").attributes
遍历这个集合
for each element in objnodes
response.write element.nodename 属性名
response.write element.nodevalue 属性值
next




...全文
26 点赞 收藏 24
写回复
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
chookrib 2003-03-06
mark
回复
dark_will 2003-03-05
mark
回复
veryhappy 2003-02-21
我用这个
set objtofind=objdom.documentElement.SelectSingleNode("//people/man")

总是出错?
回复
nittystone 2003-02-21

objdom.loadxml("<people><man>1</man></people")
set objtofind=objdom.documentElement.SelectSingleNode("//people/man")

???
回复
fenlin 2003-02-19
收藏ing......
回复
nittystone 2003-02-19
载体一次就 结贴
回复
nittystone 2003-02-18
改革错

objDom.loadxml("<people><man name=""sd""/></people>")
回复
逍遥小贼 2003-02-17
不错啊,收藏
回复
allforly 2003-02-17
<%@language=jscript%>
<%
var st = new ActiveXObject("adodb.stream")
st.Type = 1
st.Open()
st.loadFromFile(Server.MapPath("test.gif"))
var pic = st.Read(-1)
st.Close()

var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
xmldoc.loadXML('<?xml version="1.0"?><root/>');
var iPic = xmldoc.createElement("pic")
iPic.dataType = 'bin.base64'
iPic.nodeTypedValue = pic
xmldoc.documentElement.appendChild(iPic);
xmldoc.save(Server.MapPath("image.xml"))

xmldoc.async = false
xmldoc.load(Server.MapPath("image.xml"))
if (xmldoc.parseError.errorCode!=0){
Response.Write("error")
}else{
var a = xmldoc.documentElement.selectSingleNode("//pic")
Response.BinaryWrite(a.nodeTypedValue)
}
%>
回复
runmin 2003-02-17
'方法2 取得xml数据串的数据
objDom.loadxml("<people><man name="sd"></people>")

???????????????

objDom.loadxml('<people><man name="sd"></people>')

!!!!!!!!!!!!!!!
回复
runmin 2003-02-17
Properties

async* Indicates whether asynchronous download is permitted. Read/write.
attributes Contains the list of attributes for this node. Read-only.
baseName* Returns the base name for the name qualified with the namespace. Read-only.
childNodes Contains a node list containing the children (for nodes that can have children). Read-only.
dataType* Specifies the data type for this node. Read/write.
definition* Returns the definition of the node in the document type definition (DTD) or schema. Read-only.
doctype Contains the document type node that specifies the DTD for this document. Read-only.
documentElement Contains the root element of the document. Read/write.
firstChild Contains the first child of this node. Read-only.
implementation Contains the IXMLDOMImplementation object for this document. Read-only.
lastChild Returns the last child node. Read-only.
namespaceURI* Returns the Uniform Resource Identifier (URI) for the namespace. Read-only.
nextSibling Contains the next sibling of this node in the parent's child list. Read-only.
nodeName Contains the qualified name of the element, attribute, or entity reference, or a fixed string for other node types. Read-only.
nodeType Specifies the XML Document Object Model (DOM) node type, which determines valid values and whether the node can have child nodes. Read-only.
nodeTypedValue* Contains this node's value, expressed in its defined data type. Read/write.
nodeTypeString* Returns the node type in string form. Read-only.
nodeValue Contains the text associated with the node. Read/write.
ondataavailable* Specifies the event handler for the ondataavailable event. Read/write.
onreadystatechange* Specifies the event handler to be called when the readyState property changes. Read/write.
ontransformnode* Specifies the event handler for the ontransformnode event. Read/write.
ownerDocument Returns the root of the document that contains this node. Read-only.
parentNode Contains the parent node (for nodes that can have parents). Read-only.
parsed* Contains True if this node and all descendants have been parsed and instantiated; False if any nodes remain to be parsed. Read-only.
parseError* Returns an IXMLDOMParseError object that contains information about the last parsing error. Read-only.
prefix* Returns the namespace prefix. Read-only.
preserveWhiteSpace* Contains True if default processing preserves white space; False otherwise. Read/write.
previousSibling Contains the left sibling of this node. Read-only.
readyState* Indicates the current state of the XML document. Read-only.
resolveExternals* Indicates whether external definitions (resolvable namespaces, DTD external subsets, and external entity references) are to be resolved at parse time, independent of validation. Read/write.
specified* Indicates whether the node (usually an attribute) is explicitly specified or derived from a default value in the DTD or schema. Read-only.
text* Contains the text content of the node and its subtrees. Read/write.
url* Returns the canonicalized URL for the last loaded XML document. Read-only.
validateOnParse* Indicates whether the parser should validate this document. Read/write.
xml* Contains the XML representation of the node and all its descendants. Read-only.

* denotes an extension to the Worldwide Web Consortium (W3C) DOM.

Methods

abort* Aborts an asynchronous download in progress.
appendChild Appends newChild as the last child of this node.
cloneNode Creates a new node that is an exact clone of this node.
createAttribute Creates a new attribute with the specified name.
createCDATASection Creates a CDATA section node that contains the supplied data.
createComment Creates a comment node that contains the supplied data.
createDocumentFragment Creates an empty DocumentFragment object.
createElement Creates an element node using the specified name.
createEntityReference Creates a new EntityReference object.
createNode* Creates a node using the supplied type, name, and namespace.
createProcessingInstruction Creates a processing instruction node that contains the supplied target and data.
createTextNode Creates a text node that contains the supplied data.
getElementsByTagName Returns a collection of elements that have the specified name.
hasChildNodes Returns True if this node has children.
insertBefore Inserts a child node to the left of the specified node or at the end of the list.
load* Loads an XML document from the specified location.
loadXML* Loads an XML document using the supplied string.
nodeFromID* Returns the node whose ID attribute matches the supplied value.
removeChild Removes the specified child node from the list of children and returns it.
replaceChild Replaces the specified old child node with the supplied new child node in the set of children of this node, and returns the old child node.
save* Saves an XML document to the specified location.
selectNodes* Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes.
selectSingleNode* Applies the specified pattern-matching operation to this node's context and returns the first matching node.
transformNode* Processes this node and its children using the supplied Extensible Stylesheet Language Transformations (XSLT) style sheet and returns the resulting transformation.
transformNodeToObject* Processes this node and its children using the supplied XSLT style sheet and returns the resulting transformation in the supplied object.

* denotes an extension to the W3C DOM.

Events

ondataavailable* Indicates that XML document data is available.
onreadystatechange* Indicates when the readyState property changes.
ontransformnode* Fires before each node in the style sheet is applied to each node in the XML source. The ontransformnode event does not fire on DOMDocument for style sheets using the XSLT namespace—http://www.w3.org/1999/XSL/Transform. It does, however, fire on DOMDocument for style sheets using the XSL namespace—http://www.w3.org/TR/WD-xsl.
回复
ccblmpt 2003-02-17
dom对象,很好
回复
linday 2003-02-17
很好

我正想写这个
回复
ssm1226 2003-02-17
<%
dim xmldoc,node
set xmldoc=server.CreateObject("msxml2.domdocument")
xmldoc.async=false '异步传输
xmldoc.loadXML "<ROOT/>"

xmldoc.createProcessingInstruction "xml","version='1.0' encoding='gb2312'"

set node=xmlCreateNode(xmldoc,xmldoc.documentElement,"user",0,0,"")

xmlCreateNode xmldoc,node,"name",1,1,"ssm1226"
xmlCreateNode xmldoc,node,"name",0,1,"呢称:雨中人"

'*********************************************************
'函数功能:添加子节点或属性
'xmldoc--XML DOM Object
'xmldocObj--要添加目的节点对象
'EleNameOrAttName--节点名或属性名
'Key--0-节点;1-属
'ifTxt--是否有内容0-无;1-有
'Text--当ifTxt=1时有效,内容
'By '版权信息:author:ssm1226 ;data:2003-1-2;copyright:yeboss.net
'*********************************************************
function xmlCreateNode(xmldoc,xmldocObj,EleNameOrAttName,Key,ifTxt,Text)
If Key=0 then
Dim SubNode
set SubNode=xmldoc.createElement(EleNameOrAttName)
if ifTxt=1 then SubNode.text=Text
xmldocObj.appendChild SubNode
set xmlCreateNode=subNode
set SubNode=Nothing
ElseIf Key=1 then
Dim AttNode
set AttNode=xmldoc.createAttribute(EleNameOrAttName)
if ifTxt=1 then AttNode.text=Text
xmldocObj.attributes.setNamedItem AttNode
set xmlCreateNode=AttNode
set AttNode=Nothing
End If
end function

Response.Write xmldoc.xml
%>
回复
ssm1226 2003-02-17
<%
dim xmldoc,node
set xmldoc=server.CreateObject("msxml2.domdocument")
xmldoc.async=false '异步传输
xmldoc.loadXML "<ROOT/>"

xmldoc.createProcessingInstruction "xml","version='1.0' encoding='gb2312'"

set node=xmlCreateNode(xmldoc,xmldoc.documentElement,"user",0,0,"")

xmlCreateNode xmldoc,node,"name",1,1,"ssm1226"
xmlCreateNode xmldoc,node,"name",0,1,"呢称:雨中人"

'*********************************************************
'函数功能:添加子节点或属性
'xmldoc--XML DOM Object
'xmldocObj--要添加目的节点对象
'EleNameOrAttName--节点名或属性名
'Key--0-节点;1-属
'ifTxt--是否有内容0-无;1-有
'Text--当ifTxt=1时有效,内容
'By '版权信息:author:ssm1226 ;data:2003-1-2;copyright:yeboss.net
'*********************************************************
function xmlCreateNode(xmldoc,xmldocObj,EleNameOrAttName,Key,ifTxt,Text)
If Key=0 then
Dim SubNode
set SubNode=xmldoc.createElement(EleNameOrAttName)
if ifTxt=1 thenSubNode.text=Text
xmldocObj.appendChild SubNode
set xmlCreateNode=subNode
set SubNode=Nothing
ElseIf Key=1 then
Dim AttNode
set AttNode=xmldoc.createAttribute(EleNameOrAttName)
if ifTxt=1 then AttNode.text=Text
xmldocObj.attributes.setNamedItem AttNode
set xmlCreateNode=AttNode
set AttNode=Nothing
End If
end function

Response.Write xmldoc.xml
%>
回复
nittystone 2003-02-17
ti 怎么没人添加阿...
回复
allforly 2003-02-17
纠正一个小小的误差:
iPic.dataType = 'bin.base64'
改成
iPic.dataType = 'bin.hex'
的话,xml文件要小很多,虽然不知道hex和base64编码有什么区别,可是节约了空间
回复
liangzhg 2003-02-17
收藏
回复
cmsoft 2003-02-16
不错,大家来学习一下:)
回复
online 2003-02-16
收藏
回复
发动态
发帖子
ASP
创建于2007-09-28

2.8w+

社区成员

ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
申请成为版主
社区公告
暂无公告