日期:2014-05-17  浏览次数:20884 次

关于asp读取修改xml节点
shu.xml结构如下
HTML code

<?xml version="1.0" encoding="UTF-8"?>
<root>
<book backGroundColor="F2F2F2">

<pages>
<page id="1" width="400" height="200" width2="1000" height2="1500" isContainSwf="0">
<item fileType="pic1" x="0" y="0" pathname="pic1/0.jpg"/>
<item fileType="pic2" x="0" y="0" pathname="pic2/0.jpg"/>
</page>


<page id="2" width="400" height="200" width2="1000" height2="1500" isContainSwf="0">
<item fileType="pic1" x="0" y="0" pathname="pic1/1.jpg"/>
<item fileType="pic2" x="0" y="0" pathname="pic2/2.jpg"/>
</page>

<page id="3" width="400" height="200" width2="1000" height2="1500" isContainSwf="0">
<item fileType="pic1" x="0" y="0" pathname="pic1/3.jpg"/>
<item fileType="pic2" x="0" y="0" pathname="pic2/4.jpg"/>
</page>



</pages>
</book>
</root>


asp 代码如下
VBScript code


    SourceFile=SourceFolder&"\shu.xml"
    SourceFile = Server.MapPath(SourceFile)
    Set objXML = server.CreateObject("MicroSoft.XMLDom")
    objXML.load(SourceFile)

    If objXML.parseError.ErrorCode <> 0 Then
        'objXML.loadXML "<?xml version=""1.0"" encoding=""UTF-8""?><root><book><pages></pages></book></root>"
    End If

    Set objRootlist = objXML.documentElement.selectSingleNode("root")

    If objRootlist.hasChildNodes then
        id = objRootlist.lastChild.firstChild.text + 1
    Else
        id=1
    End If
    
    Set oListNode = olistbook.documentElement.selectSingleNode("pages").AppendChild(objXML.createElement("page"))'这里应该怎么写
    '这里应该怎么写

    objXML.save(SourceFile)
    Set objXML = nothing
    msg = "新闻信息成功已写入。"








需要在 pages里添加
结构类似这样的内容
<page id="3" width="400" height="200" width2="1000" height2="1500" isContainSwf="0">
<item fileType="pic1" x="0" y="0" pathname="pic1/3.jpg"/>
<item fileType="pic2" x="0" y="0" pathname="pic2/4.jpg"/>
</page>




------解决方案--------------------
VBScript code

<%
SourceFile = SourceFolder & "\shu.xml"
SourceFile = Server.MapPath(SourceFile)
SourceFile = "E:\shu.xml"
Set objXML = CreateObject("Msxml2.DOMDocument")
objXML.async              = False
objXML.validateOnParse    = False
objXML.preserveWhiteSpace = False
objXML.resolveExternals   = False
objXML.setProperty "SelectionLanguage", "XPath"
objXML.load SourceFile
If objXML.parseError.ErrorCode <> 0 Then
    objXML.loadXML "<?xml version=""1.0"" encoding=""UTF-8""?><root><book><pages></pages></book></root>"
End If
Set objRootlist = objXML.documentElement
Set oNode = objXML.selectSingleNode("//page[last()]")
If oNode Is Nothing Then
    id = 1
Else
    id = CInt(oNode.getAttribute(&