解析XML只返回第一个元素

问题描述 投票:0回答:1

我是机器人框架的XML库的新手.我试着解析一个xml来获取其中的一个值,但它只会获取第一个元素.所以我的XML是这样的。

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Header>
    <firstElement>
        <secondElement>
            <myValue>...</myValue>
            <mySecondValue>...</mySecondValue>
            ...
        </secondElement
    </firstElement>
  </S:Header>
  <S:Body>
  ...
  </S:Body>
</S:Envelope>

我的很短的机器人框架测试看起来是这样的。

 ${xml}=    Parse Xml    path/to/xml
 ${first}=    get element    ${xml}    myValue
 Log  ${first}

但当解析XML时,它的记录是这样的:

INFO : ${xml} = <Element 'Envelope' at 0x00000000042B67C8>

当然,我在解析xml时的所有尝试都失败了,我得到的结果是:

FAIL : No element matching 'myValue' found.

我做错了什么?

xml robotframework robotframework-ide
1个回答
1
投票

问题出在你用来寻找元素的xpath上,请看这里。https:/robotframework.orgrobotframeworklatestlibrariesXML.html#Finding%20elements%20with%20xpath#。

它应该是这样的。

    ${x}=    Parse Xml    ${xml}    
    ${el_my_value}=    Get Element    ${x}    .//myValue
    Log  ${el_my_value}
    ${first_text}=    Get Element Text    ${el_my_value} 

注意 .//myValue.

另外,如果你想得到元素 文字,那么你需要使用关键字 Get Element Text.

所以整个工作实例和结果。

*** Settings ***
Library    XML
Variables    ../../Resources/xml_test.py

*** Test Cases ***
Test XML Parsing
    ${x}=    Parse Xml    ${xml}    
    ${el_my_value}=    Get Element    ${x}    .//myValue
    Log  ${el_my_value}
    ${first_text}=    Get Element Text    ${el_my_value}    

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.