0

私はこのxmlを持っています:

<office:body>
<office:text>
<text:sequence-decls>
<text:sequence-decl text:display-outline-level="0" text:name="Illustration"/>
<text:sequence-decl text:display-outline-level="0" text:name="Table"/>
<text:sequence-decl text:display-outline-level="0" text:name="Text"/>
<text:sequence-decl text:display-outline-level="0" text:name="Drawing"/>
</text:sequence-decls>
<text:p text:style-name="Standard">
<office:annotation>...</office:annotation>
foobar
</text:p>
</office:text>
</office:body>

「foobar」の代わりに任意のテキストを使用できるため、elementtree でテキスト「foobar」を検索したいですか?

4

1 に答える 1

1

XML ドキュメントが次のようになっているとします (名前空間が宣言されています)。

<office:document-content xmlns:office="http://openoffice.org/2000/office"
                         xmlns:text="http://openoffice.org/2000/text">

  <office:body>
    <office:text>
      <text:sequence-decls>
        <text:sequence-decl text:display-outline-level="0" text:name="Illustration"/>
        <text:sequence-decl text:display-outline-level="0" text:name="Table"/>
        <text:sequence-decl text:display-outline-level="0" text:name="Text"/>
        <text:sequence-decl text:display-outline-level="0" text:name="Drawing"/>
      </text:sequence-decls>
      <text:p text:style-name="Standard">
        <office:annotation>...</office:annotation>
        foobar
      </text:p>
    </office:text>
  </office:body>

</office:document-content>

次に、次のプログラムを使用して「foobar」文字列を取得できます。

from xml.etree import ElementTree as ET

root = ET.parse("foobar.xml")
ann = root.find(".//{http://openoffice.org/2000/office}annotation")
print ann.tail.strip()

ここでは、ElementTree.find()メソッドを使用してoffice:annotation要素を検索し、Element.tail属性は要素の終了タグの後にテキスト コンテンツを返します。

于 2012-09-12T10:31:28.370 に答える