【问题标题】:Python Element Tree PathsPython 元素树路径
【发布时间】:2015-04-22 13:20:11
【问题描述】:

我正在尝试编写一个 python 脚本来从 XML 文档构建一个对象,但是我在理解路径的工作原理时遇到了一些麻烦。这是 XML 文档的示例:

<args format="1.0">
<shaderType>
    <tag value="pattern"/>
</shaderType>
<help>
    Computes the facing ratio of the geometry : a simple dot product between
    the camera vector and the surface normal.
</help>
<page name="Parameters" open="True">
    <param name="faceForward"
        label="Face Forward"
        type="int"
        default="1"
        widget="checkbox">
        <tags>
            <tag value="__noconnection"/>
        </tags>
        <help>The facing ratio can be negative if the normal is pointing 
        away from the camera. This will flip the normal to always give a 
        positive result.</help>
    </param>
    <param name="invert"
        label="Invert"
        type="int"
        default="0"
        widget="checkbox">
        <tags>
            <tag value="__noconnection"/>
        </tags>
        <help>Inverts the facing ratio : black becomes white and 
        vice-versa.</help>
    </param>
    <param name="gamma"
        label="Gamma"
        type="float"
        default="1.0"
        widget="default">
        <tags>
            <tag value="float"/>
        </tags>
        <help>A simple gamma function to shape the facing ratio. A value of 1.0 is 
        neutral</help>
    </param>
    <param name="bumpNormal"
        label="Bump Normal"
        type="normal"
        widget="default">
        <tags>
            <tag value="normal"/>
        </tags>
        <help>If the surface is bump-mapped, input the bump normal here. If
        not connected, the node will use ths shading normal.</help>
    </param>
</page>

<output name="resultF">
    <tags>
        <tag value ="float"/>
    </tags>
</output>

<rfmdata nodeid="1053349"
         classification="rendernode/RenderMan/pattern"/>

假设我想获取对象本身的帮助语句。我正在使用这个:

    nodeText = tree.findtext('.//args/help')
    print nodeText

然而,这将返回 None。是不是我用的路径不对?

【问题讨论】:

    标签: python xml elementtree


    【解决方案1】:

    您能否确认您所指的“树”对象是 Element 类型的根元素,而不是 ElementTree 类型的实例?

    如果是ElementTree类型,请使用getroot()方法获取根元素。

    基本上,下面的代码 sn-p 应该让你得到根元素:

    from xml.etree.ElementTree import parse
    args = parse('/path/to/file').getroot()
    

    随后,您可以使用以下内容来获取每个“帮助”元素:

    args.findall(".//help")
    

    如果您只想要根对象下的“帮助”元素:

    args.find("help")
    

    如果你想要元素中的文本:

    print args.find("help").text
    

    P.S:您上面问题中的 XML sn-p 缺少匹配的“args”结束标记

    【讨论】:

      【解决方案2】:

      使用

       nodeHelp = tree.xpath("/args/help")[0]
       print(nodeHelp.text)
      

      【讨论】:

      • 感谢您的帮助,但我确实尝试过,但它仍然返回 None。我知道文档是正确的,因为当我做一个 findall 时,我会得到所有的帮助元素。
      • @jspada 改变了我的答案。
      • 嘿 Tichodroma,感谢您的帮助,但由于元素树不支持 xpath,因此效果不佳。我确实采取了你所做的并用它来解决问题。我知道它永远是第一个元素,所以我做了一个查找全部并选择了第一个元素。
      猜你喜欢
      • 2021-08-06
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 2016-06-21
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多