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

ラファエルのアドバイスをコードで例示し、ドキュメントを読むときに探す検索/キーワードを提供するには、次のようにします。

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 に答える