Python elementtree получить XML CDATA - PullRequest
1 голос
/ 19 июня 2020

Мой вопрос в том, как go я могу получить доступ к информации, указанной ниже. Я хочу получить CDATA из свойства с именем «коробка», но, похоже, не могу понять, как это сделать. В моем полном сценарии показано, как Ive получал доступ к другим элементам в XML и как это делал ive.

<p:metadata>
                <p:property name="box">
                    <![CDATA[194.6875,50]]>
                </p:property>
                <p:property name="disabled">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="default">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="fillColor">
                    <![CDATA[#CCCCCCFF]]>
                </p:property>
                <p:property name="strokeColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="strokeStyle">
                    <![CDATA[1|]]>
                </p:property>
                <p:property name="textContent">
                    <![CDATA[exit]]>
                </p:property>
                <p:property name="textFont">
                    <![CDATA[Comic Sans MS|normal|normal|5px|none]]>
                </p:property>
                <p:property name="textColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="textAlign">
                    <![CDATA[1,1]]>
                </p:property>
            </p:metadata>

Полный сценарий

import time
import xml.etree.ElementTree as ET
import json

class TextObject:
    def __init__(self, name, x, y, content):
        self.name    = name
        self.x       = x
        self.y       = y
        self.content = content

class ButtonObject:
    def __init__(self, x, y, width, height, name):
        self.x      = x
        self.y      = y
        self.width  = width
        self.height = height
        self.name   = name

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

def main():

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

    CreateButtonObject(root)
    CreateTextObject(root)  
    print("suc")
    time.sleep(500);

def CreateTextObject(root):
    for content_ns in root.findall('p_link:Content', namespaces):
        for g_ns in content_ns.findall('text_link:g', namespaces):
            def_object = g_ns.get("{http://www.evolus.vn/Namespace/Pencil}def")
            if(IsText(def_object)):
                for text_ns in g_ns.findall("text_link:text", namespaces):
                    for text_content_ns in text_ns.findall("text_link:tspan", namespaces):
                        matrix_str = g_ns.get("transform").replace("matrix(", "").replace(")", "").split(",")
                        name = "MainMenu" + text_content_ns.text + "TextObject"
                        TextObject_1 = TextObject(name, int(float(matrix_str[4])), int(float(matrix_str[5])), text_content_ns.text)
                        AddTextObjectToJSON(TextObject_1)


def AddTextObjectToJSON(TextObject):
    print("t")
    with open('base.json') as f:
        data = json.load(f)

    data["Objects"][0]["Text"].append({"RX": TextObject.x, "RY": TextObject.y, "FontSize": 70, "Name" : TextObject.name, "Path" : "assets/font/DanielLinssenM5/m5x7.ttf", "Content" : TextObject.content, "Color" : [14, 17, 19, 255]})

    with open('base.json', 'w') as f:
        json.dump(data, f)

def CreateButtonObject(root):
    for content_ns in root.findall('p_link:Content', namespaces):
        for g_ns in content_ns.findall('text_link:g', namespaces):
            def_object = g_ns.get("{http://www.evolus.vn/Namespace/Pencil}def")
            if(IsButton(def_object)):
                for text_ns in g_ns.findall("text_link:text", namespaces):
                    matrix_str = g_ns.get("transform").replace("matrix(", "").replace(")", "").split(",")
                    name = "MainMenu" + text_ns.text + "ButtonObject"
                    ButtonObject_1 = ButtonObject(int(float(matrix_str[4])), int(float(matrix_str[5])), 1, 1, name)

                AddButtonObjectToJSON(ButtonObject_1)

def AddButtonObjectToJSON(ButtonObject):
    print("b")
    with open('base.json') as f:
        data = json.load(f)

    data["Objects"][0]["Buttons"].append({"RX": ButtonObject.x, "RY": ButtonObject.y, "Width": ButtonObject.width, "Height" : ButtonObject.height, "Name" : ButtonObject.name})

    with open('base.json', 'w') as f:
        json.dump(data, f)


def IsButton(g_def):
    if(g_def == "Evolus.Sketchy.GUI:button"):
        return True
    else:
        return False

def IsText(g_def):
    if(g_def == "Evolus.Common:PlainTextV2"):
        return True
    else:
        return False


main()

Полный XML

<p:Page xmlns:p="http://www.evolus.vn/Namespace/Pencil">
    <p:Properties>
        <p:Property name="id">25d44158885b4f0e841f19219facac65</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_25d44158885b4f0e841f19219facac65.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:Bitmap" id="fd4b37c164214759b424c8776d4b9316" transform="matrix(1,0,0,1,0,-2.842170943040401e-14)">
            <p:metadata>
                <p:property name="box">
                    <![CDATA[1920,1080]]>
                </p:property>
                <p:property name="imageData">
                    <![CDATA[1920,1080,ref://6a1d3eb6581546568a08fb0ed83a5e29.png]]>
                </p:property>
                <p:property name="withBlur">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="fillColor">
                    <![CDATA[#FFFFFF00]]>
                </p:property>
                <p:property name="strokeColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="strokeStyle">
                    <![CDATA[0|]]>
                </p:property>
            </p:metadata>
            <defs>
                <filter height="1.2558399" y="-0.12792" width="1.06396" x="-0.03198" p:name="imageShading" id="2d333893094d4546a303dc1cc65c5210">
                    <feGaussianBlur stdDeviation="1.3325" in="SourceAlpha" />
                </filter>
                <g p:name="container" id="c7b9d51bd1d54f76afc0b6d806cd55de">
                    <rect style="fill: rgb(255, 255, 255); stroke: rgb(0, 0, 0); stroke-opacity: 1; stroke-width: 0; fill-opacity: 0;" p:name="bgRect" id="fd37bae77e24456abaffa10e1f7cda91" transform="translate(0,0)" height="1080" width="1920" />
                    <g p:name="imageContainer" id="60d2a4f13d1a4adbbc755ef6b6331a63" transform="scale(1,1)">
                        <image x="0" y="0" p:name="image" id="6f60fb2a6c9546338918278ac90335b9" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="file:///C:/Users/Jax/AppData/Local/Temp/tmp-7324ixfqUB6tNRV2/refs/6a1d3eb6581546568a08fb0ed83a5e29.png?token=1592150336396" width="1920" height="1080" />
                    </g>
                </g>
            </defs>
            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#c7b9d51bd1d54f76afc0b6d806cd55de" transform="translate(1, 1)" p:filter="url(#2d333893094d4546a303dc1cc65c5210)" style="opacity: 0.6; visibility: hidden; display: none;" p:name="bgCopy" id="08cdf83ba36c4e86995edc423047d3bd" />
            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#c7b9d51bd1d54f76afc0b6d806cd55de" />
        </g>
        <g xmlns="http://www.w3.org/2000/svg" p:type="Shape" p:def="Evolus.Sketchy.GUI:button" id="b8e70d9a83f34a6b9001c72aacc4822a" transform="matrix(1,0,0,1,862.6874999999999,346.625)">
            <p:metadata>
                <p:property name="box">
                    <![CDATA[194.6875,50]]>
                </p:property>
                <p:property name="disabled">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="default">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="fillColor">
                    <![CDATA[#CCCCCCFF]]>
                </p:property>
                <p:property name="strokeColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="strokeStyle">
                    <![CDATA[1|]]>
                </p:property>
                <p:property name="textContent">
                    <![CDATA[start]]>
                </p:property>
                <p:property name="textFont">
                    <![CDATA[Comic Sans MS|normal|normal|5px|none]]>
                </p:property>
                <p:property name="textColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="textAlign">
                    <![CDATA[1,1]]>
                </p:property>
            </p:metadata>
            <g p:name="rect" id="0317e2a448bf4601bb43f85d394fe799" style="fill: rgb(204, 204, 204); fill-opacity: 1; stroke: rgb(0, 0, 0); stroke-opacity: 1; stroke-width: 1;">
                <path style="stroke-linejoin: round;" p:name="line1" id="0b05957cae0e454f80521614dd3543f3" d="M 0 0 C 65 1 130 1 194.6875 0 C 195 17 195 33 194.6875 50 C 130 49 65 49 0 50 C 0 33 0 17 0 0 z" />
            </g>
            <text p:name="text" id="3586fc452a184851a6defab016b1f945" transform="translate(91,27)" style="font-family: &quot;Comic Sans MS&quot;; font-size: 5px; font-weight: normal; font-style: normal; text-decoration: none; fill: rgb(0, 0, 0); fill-opacity: 1;">start</text>
        </g>
        <g xmlns="http://www.w3.org/2000/svg" xmlns:p="http://www.evolus.vn/Namespace/Pencil" p:type="Shape" p:def="Evolus.Sketchy.GUI:button" id="0ae85861536b4717961aacf55d823d35" transform="matrix(1,0,0,1,862.6874999999999,460.625)">
            <p:metadata>
                <p:property name="box">
                    <![CDATA[194.6875,50]]>
                </p:property>
                <p:property name="disabled">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="default">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="fillColor">
                    <![CDATA[#CCCCCCFF]]>
                </p:property>
                <p:property name="strokeColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="strokeStyle">
                    <![CDATA[1|]]>
                </p:property>
                <p:property name="textContent">
                    <![CDATA[options]]>
                </p:property>
                <p:property name="textFont">
                    <![CDATA[Comic Sans MS|normal|normal|5px|none]]>
                </p:property>
                <p:property name="textColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="textAlign">
                    <![CDATA[1,1]]>
                </p:property>
            </p:metadata>
            <g p:name="rect" id="4bdd20ba79924c25899ddef6ebe8178a" style="fill: rgb(204, 204, 204); fill-opacity: 1; stroke: rgb(0, 0, 0); stroke-opacity: 1; stroke-width: 1;">
                <path style="stroke-linejoin: round;" p:name="line1" id="f0ecb383f5ee4d6c91b40b3d2c59e7fc" d="M 0 0 C 65 1 130 1 194.6875 0 C 195 17 195 33 194.6875 50 C 130 49 65 49 0 50 C 0 33 0 17 0 0 z" />
            </g>
            <text p:name="text" id="cbc74b4807834b5d976af859aa5286df" transform="translate(89,27)" style="font-family: &quot;Comic Sans MS&quot;; font-size: 5px; font-weight: normal; font-style: normal; text-decoration: none; fill: rgb(0, 0, 0); fill-opacity: 1;">options</text>
        </g>
        <g xmlns="http://www.w3.org/2000/svg" xmlns:p="http://www.evolus.vn/Namespace/Pencil" p:type="Shape" p:def="Evolus.Sketchy.GUI:button" id="ae8b8875e1404436bd8135762d6098ca" transform="matrix(1,0,0,1,862.6874999999999,573.625)">
            <p:metadata>
                <p:property name="box">
                    <![CDATA[194.6875,50]]>
                </p:property>
                <p:property name="disabled">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="default">
                    <![CDATA[false]]>
                </p:property>
                <p:property name="fillColor">
                    <![CDATA[#CCCCCCFF]]>
                </p:property>
                <p:property name="strokeColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="strokeStyle">
                    <![CDATA[1|]]>
                </p:property>
                <p:property name="textContent">
                    <![CDATA[exit]]>
                </p:property>
                <p:property name="textFont">
                    <![CDATA[Comic Sans MS|normal|normal|5px|none]]>
                </p:property>
                <p:property name="textColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="textAlign">
                    <![CDATA[1,1]]>
                </p:property>
            </p:metadata>
            <g p:name="rect" id="34d7f2e6ddfa4804bb6c79432bfaa516" style="fill: rgb(204, 204, 204); fill-opacity: 1; stroke: rgb(0, 0, 0); stroke-opacity: 1; stroke-width: 1;">
                <path style="stroke-linejoin: round;" p:name="line1" id="385ea72a33e44347b8cb48f47e1e9292" d="M 0 0 C 65 1 130 1 194.6875 0 C 195 17 195 33 194.6875 50 C 130 49 65 49 0 50 C 0 33 0 17 0 0 z" />
            </g>
            <text p:name="text" id="837cac256ac84e56bc4a01cee696f2d5" transform="translate(93,27)" style="font-family: &quot;Comic Sans MS&quot;; font-size: 5px; font-weight: normal; font-style: normal; text-decoration: none; fill: rgb(0, 0, 0); fill-opacity: 1;">exit</text>
        </g>
        <g xmlns="http://www.w3.org/2000/svg" p:type="Shape" p:def="Evolus.Common:PlainTextV2" p:sc="Label" id="9eb177b569bc41cda659dad7c9fcfe9f" transform="matrix(1,0,0,1,918.5624999999999,382.8125)">
            <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[Start]]>
                </p:property>
                <p:property name="textColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="textFont">
                    <![CDATA[Arial Black|normal|normal|30px|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="53a89ef724c9478d9c944ace687c94d3" width="83.109375" height="42.171875" />
            <text xml:space="preserve" p:name="text" id="cfe29c0f93334914b7f434d2bc3f8e8b" style="fill: rgb(0, 0, 0); fill-opacity: 1; font-family: &quot;Arial Black&quot;; font-size: 30px; font-weight: normal; font-style: normal; text-decoration: none;">
                <tspan x="0" y="0">Start</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="7177d58cf0c04ab5b55c948cfde65cc0" transform="matrix(1,0,0,1,897.5624999999999,496.8125)">
            <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[Options]]>
                </p:property>
                <p:property name="textColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="textFont">
                    <![CDATA[Arial Black|normal|normal|30px|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="65feaf8d86df49fe8c3f527ec1ad24b8" width="126.640625" height="42.171875" />
            <text xml:space="preserve" p:name="text" id="a1e61e2540fe4485b323a7be48f42af6" style="fill: rgb(0, 0, 0); fill-opacity: 1; font-family: &quot;Arial Black&quot;; font-size: 30px; font-weight: normal; font-style: normal; text-decoration: none;">
                <tspan x="0" y="0">Options</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="c3df718c0845486dbf5d5b1a4e611cac" transform="matrix(1,0,0,1,927.5624999999999,609.8125)">
            <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[Exit]]>
                </p:property>
                <p:property name="textColor">
                    <![CDATA[#000000FF]]>
                </p:property>
                <p:property name="textFont">
                    <![CDATA[Arial Black|normal|normal|30px|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="843ecbddd52d43079ad5fed4bf7c06f3" width="64.96875" height="42.171875" />
            <text xml:space="preserve" p:name="text" id="00db4779195e434daea0f4f1fdbe9ba8" style="fill: rgb(0, 0, 0); fill-opacity: 1; font-family: &quot;Arial Black&quot;; font-size: 30px; font-weight: normal; font-style: normal; text-decoration: none;">
                <tspan x="0" y="0">Exit</tspan>
            </text>
        </g>
    </p:Content>
</p:Page>
...