代码之家  ›  专栏  ›  技术社区  ›  Bobby Brown

如何在vbscript中访问元素XML的第二个子节点?

  •  -1
  • Bobby Brown  · 技术社区  · 8 年前

    在互联网上花了很多小时后,我无法让它工作,需要一些帮助。

    XML:

    xml snippet highlighting NoteSynopsis

    代码:

    set xmlDoc  = SERVER.CREATEOBJECT("MSXML2.DomDocument.6.0")
    xmlDoc.async = False              xmlDoc.Load("D:\GVPApplications\TechSupportCreateIncident\CreateIncidentRequest.xml")    
    xmlDoc.setProperty "NewParser","true"               
    
    set Root = xmlDoc.documentElement
    
    set NodeList1 = Root.getElementsByTagName("IncidentNote")
    
                     For Each Elem in NodeList1
                         if Elem.firstChild.nodename = "Note" then
                              Elem.firstChild.text = Notes
                         end if
    
                     Next
    

    问题: 正如图像XML中强调的那样,我需要能够读取“NoteSynopsis”元素及其值。虽然这看起来很简单,但我还没有找到解决办法。如果这是最后一个没有问题的孩子,我会做Elem。最后一个孩子。nodename,但这不是!

    1 回复  |  直到 8 年前
        1
  •  0
  •   mortb    8 年前

    您可以使用 selectSingleNode(...) 方法并使用XPath

    set xmlDoc  = SERVER.CREATEOBJECT("MSXML2.DomDocument.6.0")
    xmlDoc.async = False              xmlDoc.Load("D:\GVPApplications\TechSupportCreateIncident\CreateIncidentRequest.xml")    
    xmlDoc.setProperty "NewParser","true"
    xmlDocument.setProperty "SelectionLanguage", "XPath"               
    
    Dim firstIncidentNote
    set firstIncidentNote = xmlDoc.SelectSingleNode("//IncidentNote")
    Dim note
    Dim noteSynopsis
    set note = firstIncidentNote.GetAttribute("Note")
    set note = firstIncidentNote.GetAttribute("NoteSynopsis")
    

    https://msdn.microsoft.com/en-us/library/ms757846(v=vs.85).aspx

    推荐文章