元素树无法访问没有名称空间的元素

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

我正在尝试使用元素树访问此XML文件中的元素,但我无法进入它。它甚至不识别它。

            for text in g_ns.findall("text"):
                print("textfound")

这是似乎不太行的两行。

import time
import xml.etree.ElementTree as ET

def main():

    tree = ET.parse('threetext.xml')
    root = tree.getroot()

    namespaces = {'p_link': 'http://www.evolus.vn/Namespace/Pencil',
                  'text_link': 'http://www.w3.org/2000/svg'}

    for content_ns in root.findall('p_link:Content', namespaces):
        print("content_ns Found!")
        for g_ns in content_ns.findall('text_link:g', namespaces):
            print("Id: " + g_ns.get("id") + "  Cords:"+ g_ns.get("transform"))
            for text in g_ns.findall("text"):
                print("textfound")

    print("suc")
    time.sleep(500);

main()

这是我正在使用的XML

<p:Page xmlns:p="http://www.evolus.vn/Namespace/Pencil">
    <p:Properties>
        <p:Property name="id">42b530d5608c450a80cf76c2ef18da41</p:Property>
        <p:Property name="name">MainMenu</p:Property>
        <p:Property name="width">1920</p:Property>
        <p:Property name="height">1080</p:Property>
        <p:Property name="pageFileName">page_42b530d5608c450a80cf76c2ef18da41.xml</p:Property>
        <p:Property name="zoom">0.64</p:Property>
    </p:Properties>
    <p:Content>
        <g xmlns="http://www.w3.org/2000/svg" p:type="Shape" p:def="Evolus.Common:PlainTextV2" p:sc="Label" id="9d524b3833bc48c689d55465be6efda6" transform="matrix(1,0,0,1,63,70.125)">
            <p:metadata>
                <p:property name="disabled">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="width">
                    <![CDATA[100,0]]>
                </p:property>
                <p:property name="fixedWidth">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="label">
                    <![CDATA[Project Stocks]]>
                </p:property>
                <p:property name="textColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="textFont">
                    <![CDATA['Comic Sans MS'|normal|normal|12px|none]]>
                </p:property>
                <p:property name="textAlign">
                    <![CDATA[0,0]]>
                </p:property>
            </p:metadata>
            <rect x="0" y="0" style="fill: none; stroke: none; visibility: hidden; display: none;" p:name="bgRect" id="6acc2d002b4b4613a9f046b910de1154" width="83.90625" height="15.984375" />
            <text xml:space="preserve" p:name="text" id="a245cae7bbf54f649c226cdd5271002e" style="fill: rgb(0, 0, 0); fill-opacity: 1; font-family: &quot;Comic Sans MS&quot;; font-size: 12px; font-weight: normal; font-style: normal; text-decoration: none;">
                <tspan x="0" y="0">Project Stocks</tspan>
            </text>
        </g>
        <g xmlns="http://www.w3.org/2000/svg" xmlns:p="http://www.evolus.vn/Namespace/Pencil" p:type="Shape" p:def="Evolus.Common:PlainTextV2" p:sc="Label" id="fe1b7e3db04b48f6990561045f3cd02b" transform="matrix(1,0,0,1,63,127.125)">
            <p:metadata>
                <p:property name="disabled">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="width">
                    <![CDATA[100,0]]>
                </p:property>
                <p:property name="fixedWidth">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="label">
                    <![CDATA[Project Stocks]]>
                </p:property>
                <p:property name="textColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="textFont">
                    <![CDATA['Comic Sans MS'|normal|normal|12px|none]]>
                </p:property>
                <p:property name="textAlign">
                    <![CDATA[0,0]]>
                </p:property>
            </p:metadata>
            <rect x="0" y="0" style="fill: none; stroke: none; visibility: hidden; display: none;" p:name="bgRect" id="c2537095c3d64a55a1b5c9bb5ef74bf6" width="83.90625" height="15.984375" />
            <text xml:space="preserve" p:name="text" id="f0f57d36120d47dbbdba20a49a15e2e1" style="fill: rgb(0, 0, 0); fill-opacity: 1; font-family: &quot;Comic Sans MS&quot;; font-size: 12px; font-weight: normal; font-style: normal; text-decoration: none;">
                <tspan x="0" y="0">Project Stocks</tspan>
            </text>
        </g>
        <g xmlns="http://www.w3.org/2000/svg" xmlns:p="http://www.evolus.vn/Namespace/Pencil" p:type="Shape" p:def="Evolus.Common:PlainTextV2" p:sc="Label" id="519274fbdbe6454e96662fdfc078edcc" transform="matrix(1,0,0,1,63,212.125)">
            <p:metadata>
                <p:property name="disabled">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="width">
                    <![CDATA[100,0]]>
                </p:property>
                <p:property name="fixedWidth">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="label">
                    <![CDATA[Project Stocks]]>
                </p:property>
                <p:property name="textColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="textFont">
                    <![CDATA['Comic Sans MS'|normal|normal|12px|none]]>
                </p:property>
                <p:property name="textAlign">
                    <![CDATA[0,0]]>
                </p:property>
            </p:metadata>
            <rect x="0" y="0" style="fill: none; stroke: none; visibility: hidden; display: none;" p:name="bgRect" id="e8850b49953e483584a41f7605f45b27" width="83.90625" height="15.984375" />
            <text xml:space="preserve" p:name="text" id="0b37e2dcfc5e4db2a63cd23ed3f2c3e7" style="fill: rgb(0, 0, 0); fill-opacity: 1; font-family: &quot;Comic Sans MS&quot;; font-size: 12px; font-weight: normal; font-style: normal; text-decoration: none;">
                <tspan x="0" y="0">Project Stocks</tspan>
            </text>
        </g>
    </p:Content>
</p:Page>

这是打印到控制台的内容

content_ns Found!
Id: 9d524b3833bc48c689d55465be6efda6  Cords:matrix(1,0,0,1,63,70.125)
Id: fe1b7e3db04b48f6990561045f3cd02b  Cords:matrix(1,0,0,1,63,127.125)
Id: 519274fbdbe6454e96662fdfc078edcc  Cords:matrix(1,0,0,1,63,212.125)
suc
python python-3.x xml parsing elementtree
1个回答
0
投票

感谢@mzjn的帮助,这是正确的

import time
import xml.etree.ElementTree as ET

def main():

    tree = ET.parse('menu.xml')
    root = tree.getroot()

    namespaces = {'p_link': 'http://www.evolus.vn/Namespace/Pencil',
                  'text_link': 'http://www.w3.org/2000/svg'}

    for content_ns in root.findall('p_link:Content', namespaces):
        print("content_ns Found!")
        for g_ns in content_ns.findall('text_link:g', namespaces):
            print("Id: " + g_ns.get("id") + "  Cords:"+ g_ns.get("transform"))
            for text in g_ns.findall("text_link:text", namespaces):
                print("textfound")

    print("suc")
    time.sleep(500);

main()
© www.soinside.com 2019 - 2024. All rights reserved.