why does Msxml DocumentElement/SelectSingleNode returns nothing?

房东的猫 提交于 2020-01-05 03:27:33

问题


The DocumentElement property and the SelectSingleNode keep on returning nothing, I have verified that the xml loads correctly, the problem seems to lie in the xml parser. The xml does not have any namespaces so it shouldn't needed to be set.

Private Function ParseWord(word As String) As String
    Dim tempFile As String
    tempFile = Environ("temp") & "\" & "temporaryWord" & ".xml"
    Call CreateFile(tempFile, word)

    Dim xmlDoc As Object
    Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0")

    With xmlDoc
     .async = False
     .setProperty "SelectionLanguage", "XPath"
     .validateOnParse = False
     .Load tempFile
     '.setProperty "SelectionNamespaces", ""
     '.Namespaces = False
    End With

    Dim xmlElement As Object
    Set xmlElement = xmlDoc.DocumentElement

    If xmlElement Is Nothing Then
        MsgBox "error in element"
        Exit Function
    End If

    Dim nodeXML As Object
    Set nodeXML = xmlElement.SelectSingleNode("/definitions/definition/text")

    If nodeXML Is Nothing Then
        MsgBox "error"
    Else
        MsgBox nodeXML.Text
        ParseWord = nodeXML.Text
    End If

End Function

xml source :http://api.wordnik.com/v4/word.xml/intransigent/definitions?limit=200&includeRelated=true&useCanonical=true&includeTags=false&api_key=a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><definitions><definition sequence="0">        <textProns/><sourceDictionary>ahd-legacy</sourceDictionary><exampleUses/><relatedWords/><labels/>   <citations/><word>intransigent</word><attributionText>from The American Heritage® Dictionary of the English Language, 4th Edition</attributionText><text>Refusing to moderate a position, especially an extreme position; uncompromising.</text><partOfSpeech>adjective</partOfSpeech><score>0.0</score></definition></definitions>

The createFile function is from here:

http://www.jpsoftwaretech.com/vba/msxml-object-library-routines/#createfile


回答1:


Loading the xml from a string works for me.(maybe an string encoding issue?)

Sub test()
    'Cell A1 contains the xml
    ParseXML (Range("A1"))
End Sub
Private Function ParseXML(xmlString As String) As String
    Dim tempFile As String

    Dim xmlDoc As Object
    Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0")

    With xmlDoc
     .async = False
     .setProperty "SelectionLanguage", "XPath"
     .validateOnParse = False
     .LoadXML xmlString
     '.setProperty "SelectionNamespaces", ""
     '.Namespaces = False
    End With

    Dim xmlElement As Object
    Set xmlElement = xmlDoc.DocumentElement

    If xmlElement Is Nothing Then
        MsgBox "error in element"
        Exit Function
    End If

    Dim nodeXML As Object
    Set nodeXML = xmlElement.SelectSingleNode("/definitions/definition/text")

    If nodeXML Is Nothing Then
        MsgBox "error"
    Else
        MsgBox nodeXML.Text
        ParseXML = nodeXML.Text
    End If

End Function


来源:https://stackoverflow.com/questions/27645044/why-does-msxml-documentelement-selectsinglenode-returns-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!