1

我需要删除 XML 中的一个节点,我的 XML 如下所示

<message_version>Test</message_version>
<responder_id>My test XML file</responder_id>
</m_control>
<m_content>
<b_control>
    <quote_response_status>error</quote_response_status>
    <quote_error_note>
        <reason>
            <note>
                <text>An error occurred.
                </text>
            </note>
        </reason>
    </quote_error_note>
</b_control>

在上面的 xml 中,我需要删除节点“文本”和“注释”,并保留文本“发生错误”。原封不动。我怎样才能做到这一点。

基本上,在删除节点“文本”和“注释”后,我需要如下 XML

<message_version>Test</message_version>
<responder_id>My test XML file</responder_id>
</m_control>
<m_content>
<b_control>
    <quote_response_status>error</quote_response_status>
    <quote_error_note>
        <reason>
            An error occurred.
        </reason>
    </quote_error_note>
</b_control>

我尝试了以下但没有成功:

sXMLPath = "//message/m_content/b_control/quote_error_note/reason"
Set sRemoveText = m_objResponseXML.selectSingleNode(sXMLPath & "/note/text")
Set sRemoveNote = m_objResponseXML.selectSingleNode(sXMLPath & "/note")

If Not sRemoveText is Nothing Then
    m_objResponseXML.selectSingleNode(sXMLPath & "/note").removeChild(sRemoveText)
    sErrorMsg = GetNodeText(sXMLPath & "/note/text", m_objResponseXML)
End if
If Not sRemoveText is Nothing Then
    m_objResponseXML.selectSingleNode(sXMLPath).removeChild(sRemoveNote)
End If

If sErrorMsg <> "" Then
    m_objResponseXML.selectSingleNode(sXMLPath).text = sErrorMsg
End If

任何形式的帮助都深表感谢。

4

2 回答 2

3

用代码举例说明 Rafael 的建议,并在阅读文档时为您提供一些搜索/关键字:

Option Explicit

Dim oXML : Set oXML = CreateObject("MSXML2.DomDocument")
If oXML.load(".\005.xml") Then
   WScript.Echo oXML.xml
   Dim ndReason : Set ndReason = oXML.selectSingleNode("/b_control/quote_error_note/reason")
   If ndReason Is Nothing Then
      WScript.Echo "not reasonable"
   Else
      Dim sReason : sReason = ndReason.selectSingleNode("note/text").text
      ndReason.removeChild ndReason.firstChild
      WScript.Echo oXML.xml
      ndReason.appendChild oXML.createTextNode(sReason)
      WScript.Echo oXML.xml
   End If
Else
   WScript.Echo oXML.parseError.reason
End If

输出:

cscript 005.vbs
<b_control>
        <quote_response_status>error</quote_response_status>
        <quote_error_note>
                <reason>
                        <note>
                                <text>An error occurred.
                </text>
                        </note>
                </reason>
        </quote_error_note>
</b_control>

<b_control>
        <quote_response_status>error</quote_response_status>
        <quote_error_note>
                <reason>
                </reason>
        </quote_error_note>
</b_control>

<b_control>
        <quote_response_status>error</quote_response_status>
        <quote_error_note>
                <reason>An error occurred.</reason>
        </quote_error_note>
</b_control>
于 2012-10-31T19:02:22.377 回答
0

以您想要的方式删除数据,违背了 XML/HTML 层次结构哲学,其中每个标记都包含在另一个标记中。

简单的解决方案是复制字符串删除标签并填充原因标签的文本属性来实现这一点。

否则会变坏

要操作 XML,您可以使用MSXML 对象参考

编辑

基于所示示例

sXMLPath = "//message/m_content/b_control/quote_error_note/reason"

你的 XPath 字符串不正确,它是正确的

sXMLPath = "//b_control/quote_error_note/reason"
于 2012-10-31T14:20:29.083 回答