当前位置: 代码迷 >> ASP >> 关于asp读取批改xml节点
  详细解决方案

关于asp读取批改xml节点

热度:438   发布时间:2012-08-21 13:00:21.0
关于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("id")) + 1
End If

Set oPage = objXML.createElement("page")
oPage.setAttribute "id", id
oPage.setAttribute "width", "400"
oPage.setAttribute "height", "200"
oPage.setAttribute "width2", "1000"
oPage.setAttribute "height2", "1500"
oPage.setAttribute "isContainSwf", "0"
Set oItem = objXML.createElement("item")
oItem.setAttribute "fileType", "pic1"
oItem.setAttribute "x", "0"
oItem.setAttribute "y", "0"
oItem.setAttribute "pathname", "pic1/3.jpg"
oPage.appendChild oItem
Set oItem = objXML.createElement("item")
oItem.setAttribute "fileType", "pic2"
oItem.setAttribute "x", "0"
oItem.setAttribute "y", "0"
oItem.setAttribute "pathname", "pic2/4.jpg"
oPage.appendChild oItem

Set oNode = objXML.selectSingleNode("//root/book/pages")
oNode.appendChild oPage
objXML.save SourceFile

Set oNode = Nothing
Set oItem = Nothing
Set oPage = Nothing
Set objXML = Nothing
msg = "新闻信息成功已写入。"

%> 
  相关解决方案