首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > ASP >

关于asp读取批改xml节点

2012-08-21 
关于asp读取修改xml节点shu.xml结构如下HTML code?xml version1.0 encodingUTF-8?rootbook bac

关于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              = FalseobjXML.validateOnParse    = FalseobjXML.preserveWhiteSpace = FalseobjXML.resolveExternals   = FalseobjXML.setProperty "SelectionLanguage", "XPath"objXML.load SourceFileIf objXML.parseError.ErrorCode <> 0 Then    objXML.loadXML "<?xml version=""1.0"" encoding=""UTF-8""?><root><book><pages></pages></book></root>"End IfSet objRootlist = objXML.documentElementSet oNode = objXML.selectSingleNode("//page[last()]")If oNode Is Nothing Then    id = 1Else    id = CInt(oNode.getAttribute("id")) + 1End IfSet oPage = objXML.createElement("page")oPage.setAttribute "id", idoPage.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 oItemSet oItem = objXML.createElement("item")oItem.setAttribute "fileType", "pic2"oItem.setAttribute "x", "0"oItem.setAttribute "y", "0"oItem.setAttribute "pathname", "pic2/4.jpg"oPage.appendChild oItemSet oNode = objXML.selectSingleNode("//root/book/pages")oNode.appendChild oPageobjXML.save SourceFileSet oNode = NothingSet oItem = NothingSet oPage = NothingSet objXML = Nothingmsg = "新闻信息成功已写入。"%> 

热点排行