为什么Msxml DocumentElement / SelectSingleNode什么也不返回? [英] why does Msxml DocumentElement/SelectSingleNode returns nothing?

查看:195
本文介绍了为什么Msxml DocumentElement / SelectSingleNode什么也不返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DocumentElement属性和SelectSingleNode不会返回任何内容,我已经验证了xml正确加载,问题似乎在于xml解析器。 xml没有任何命名空间,因此不需要设置。

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源: http:// app b

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>

createFile函数来自这里:

The createFile function is from here:

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

推荐答案

从字符串加载xml对我来说(可能是一个字符串编码问题?)

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

这篇关于为什么Msxml DocumentElement / SelectSingleNode什么也不返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆