1

我的 XML 文件:

<?xml version="1.0"?>
<root>
<msg>
    <MessageError>
        <BookingID>123</BookingID>
        <Error>Invalid patient name</Error>
        <Error>PATIENT NOT FOUND</Error>
        <Message>Incoming MESSAGE DATA 1</Message>
    </MessageError>
    <MessageError>
        <BookingID>456</BookingID>
        <Error>Undefined patient account number.</Error>
        <Error>Undefined Account Number</Error>
        <Message>Incoming MESSAGE DATA 2</Message>
    </MessageError>
    <MessageError>
        <BookingID>789</BookingID>
        <Error>DOB invalid</Error>
        <Message>Incoming MESSAGE DATA 3</Message>
    </MessageError>
</msg>
</root>

我的 tcl 脚本:

        set dom [dom parse $msg]
        set root [$dom documentElement]         

        set MessageError [$root selectNodes "/root/msg/MessageError" ]
        foreach node $MessageError {
            set Error [$root selectNodes {/root/msg/MessageError/Error} ]
            #set bookingid [$MessageError text]
            #echo "BookingIDXML - $bookingid"
            #set message [$MessageError text]
            #echo "MessageXML - $message"

            foreach errornode $Error {
                set error [$errornode text]
                echo "ErrorXML - $error"
            }
        }

我的输出:

ErrorXML - Invalid patient name
ErrorXML - PATIENT NOT FOUND
ErrorXML - Undefined patient account number.
ErrorXML - Undefined Account Number
ErrorXML - DOB invalid
ErrorXML - Invalid patient name
ErrorXML - PATIENT NOT FOUND
ErrorXML - Undefined patient account number.
ErrorXML - Undefined Account Number
ErrorXML - DOB invalid
ErrorXML - Invalid patient name
ErrorXML - PATIENT NOT FOUND
ErrorXML - Undefined patient account number.
ErrorXML - Undefined Account Number
ErrorXML - DOB invalid

互联网上缺乏使用这个强大工具的文档。我如何获得输出?我的代码中带注释的“#”部分不起作用。

BookingIDXML - 123
ErrorXML - Invalid patient name
MessageXML - Incoming MESSAGE DATA 1

BookingIDXML - 123
ErrorXML - PATIENT NOT FOUND
MessageXML - Incoming MESSAGE DATA 1

BookingIDXML - 456
ErrorXML - Undefined patient account number.
MessageXML - Incoming MESSAGE DATA 2

BookingIDXML - 465
ErrorXML - Undefined Account Number
MessageXML - Incoming MESSAGE DATA 2

BookingIDXML - 789
ErrorXML - DOB invalid
MessageXML - Incoming MESSAGE DATA 3

提前致谢。

4

2 回答 2

3

selectNodes方法使用 XPath(有很好的文档)来查找要返回的结果,上下文节点是您调用该方法的对象。因此,要找到Error特定的节点MessageError,您必须从正确的点开始。特别是,您可能希望代码执行以下操作:

foreach messageError [$root selectNodes "/root/msg/MessageError"] {
    # Print some general info (to separate error groups)
    set bookingID [lindex [$messageError selectNodes "BookingID"] 0]
    puts "ID: [$bookingID text]"
    set message [lindex [$messageError selectNodes "Message"] 0]
    puts "Message: [$message text]"
    # Print the errors
    foreach error [$messageError selectNodes "Error"] {
        puts "Error: [$error text]"
    }
}

如果您愿意,可以使用./Error而不是Error作为 XPath 搜索词;效果是一样的,但它看起来更像是一条路径。您不应该做的是从文档的根目录重新开始搜索(就像/root/msg/MessageError/Error会做的那样),因为这样您就会找到与该路径匹配的所有内容,而不仅仅是当前子上下文中的位。(子上下文有点像文件系统中的当前目录,元素有点像目录;这是部分类比——DOM 树不是目录——但还是有点类比。)

于 2013-04-26T12:16:33.777 回答
0

You have to reference the $errornode DOM nodes in your code, e.g.:

        foreach errornode $Error {
            set bookingid [[$errornode selectNodes "../BookingID"] text]
            set error [$errornode text]
            set message [[$errornode selectNodes "../Message"] text]
            puts "BookingIDXML - $bookingid"
            puts "ErrorXML - $error"
            puts "MessageXML - $message"
            puts ""
        }

But be aware: The selectNodes method will return a list when more than one node matches the query (e.g. $errornode selectNodes "../Message"). If that's the case you have to use the text method with each single list element.

于 2013-04-26T11:58:49.920 回答