2016-04-18 78 views
0

我有一個XPath查詢//*[local-name()='Home Query Data'],測試here。 現在我必須執行此XPath才能返回<Home Query Data> --- </Home Query Data>標記中的所有文本。XML DOM在標籤內獲取文本

我應該用什麼DOM選擇器從標籤中選擇整個文本? 像:objMSXML.selectNodes(XPath)

編輯

我使用的VBScript我的編程。我的代碼如下:

Sub ReadXml(FileName) 
    Dim nodeinfo (4) 
    Dim sXPath 
    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set fileObj = fso.GetFile(FileName) 
    objMSXML.async = True 
    objMSXML.load FileName 

    If 0 = objMSXML.parseError Then 
    sXPath = "//*[local-name()='Home Query Data']" 
    End If 
    Dim querySubject : Set querySubject = objMSXML.selectSingleNode(sXPath) 
    If querySubject Is Nothing Then 
    MsgBox sXPath, "failed" ** Error type mismatch [string "failed"]** 
    Else 
    For Each node In objMSXML.selectNodes(sXPath) 
     MsgBox node 
    Next 
    End If 
End Sub 

我抄它的一些其他職位,無法弄清楚如何將XML標籤內返回全部文本。

我在這部分的問題:

Dim querySubject : Set querySubject = objMSXML.selectSingleNode(sXPath) 
If querySubject Is Nothing Then 
    MsgBox sXPath, "failed" 
Else 
    For Each node In objMSXML.selectNodes(sXPath) 
    MsgBox node 
    Next 
End If 

EDIT2

我的工作XML是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<breakfast_menu> 
<food> 
    <name locale="en">Stage Query Data</name> 
    <price>$5.95</price> 
    <description>Our famous Belgian Waffles with plenty of real maple syrup</description> 
    <calories>650</calories> 
</food> 
<drink> 
    <name locale="en">Home Query Data</name> 
    <price>$4.50</price> 
    <description>Thick slices made from our homemade sourdough bread</description> 
    <calories>600</calories> 
</drink> 
<mix> 
    <name locale="en">Report Query Data</name> 
    <price>$6.95</price> 
    <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description> 
    <calories>950</calories> 
</mix> 
</breakfast_menu> 

我需要標籤<drink>名稱爲內捕獲文本Home Query Data

<name locale="en">Home Query Data</name> 
     <price>$4.50</price> 
     <description>Thick slices made from our homemade sourdough bread</description> 
     <calories>600</calories> 

目前我在MSGBOX sXPath饒人, 「失敗」 **錯誤類型不匹配[字符串 「失敗」] **

+0

你試過'node.text'? https://msdn.microsoft.com/en-us/library/ms763798.aspx –

+0

xpath的問題。它表示類型不匹配sxpath .... –

+0

顯示完整的錯誤消息。還顯示您的XML數據的代表(工作)樣本。 –

回答

1

您應該使用text()屬性選擇,而不是local-name()功能。後者獲取節點的名稱,而不是其內容。

此外,您的MsgBox呼叫無效 - 請參閱https://msdn.microsoft.com/en-us/library/sfw6660x%28v=vs.84%29.aspx

預期以下工作:

Sub ReadXML(FileName) 
    Dim sXPath 
    Set objMSXML = CreateObject("Msxml2.DOMDocument.6.0") 
    objMSXML.load FileName 

    If objMSXML.parseError = 0 Then 
    sXPath = "//*[name/text()='Home Query Data']" 
    Dim querySubject : Set querySubject = objMSXML.selectSingleNode(sXPath) 
    If querySubject Is Nothing Then 
     MsgBox sXPath, 0, "failed" 
    Else 
     For Each node In objMSXML.selectNodes(sXPath) 
     MsgBox node.text 
     Next 
    End If 
    Else 
    MsgBox objMSXML.parseError 
    End If 
End Sub 
+0

'objMSXML.selectSingleNode(sXPath)'錯了,它不返回節點。 –

+0

需要'selectSingleNode(sXPath)'以外的其他方法 –