comet.xmltree

Reading XML files or strings for XML queries.

Methods

comet.xmltree.open(path, useTreeBuffer=True)

Open an XML free from a file

Parameters:
  • path (str) –

    The full path to the XML file.

    In XML offline, SOAP and AEM® connections, XML configuration data such as panelstatements.xml and actions.xml can also be read here.

  • useTreeBuffer (bool) –

    Reading XML files can be time-consuming.

    Read files can therefore be buffered.

    Buffered XML data is deleted when the connection is disconnected.

    • True

      Buffer the input

    • False:

      Don’t buffer the input

Returns:

The tree loaded from the file

Return type:

CXMLTree

Raises:
Available:

InDesign® comet_pdf® Illustrator®

CScript:

xmlquery::open

comet.xmltree.parse(input)

Parse an XML Tree from an input string.

Parameters:

input (str) – The input XML string

Returns:

The tree loaded from the string

Return type:

CXMLTree

Raises:
  • TypeError – When parameter types are invalid

  • ValueError

    • When parameter input is empty

    • When the input could not be parsed

  • CometError – On internal error

Available:

InDesign® comet_pdf® Illustrator®

CScript:

xmlquery::parse

Examples:

Parse a CXMLTree from an XML string and walk through all nodes in preorder fashion, outputting their names to the logfile.

#!py
#pragma plain

import comet

data = """<data>
    <products>
        <product>
            <id>1</id>
            <productgroup>1</productgroup>
            <label>Orange juice</label>
            <attributes>
                <name>
                    <default>Orange juice</default>
                    <deDE>Orangensaft</deDE>
                    <frFR>Jus d‘orange</frFR>
                </name>
                <ingredients>
                    <default>&lt;b&gt;Ingredients:&lt;/b&gt; Orange juice from concentrate.</default>
                    <deDE>&lt;b&gt;Zutaten:&lt;/b&gt; Orangensaft aus Konzentrat.</deDE>
                    <frFR>&lt;b&gt;Ingrédients:&lt;/b&gt; jus d‘orange à partir de concentré</frFR>
                </ingredients>

                <energy>
                    <default>Energy</default>
                    <deDE>Energie</deDE>
                    <frFR>Énergie</frFR>
                </energy>
            </attributes>
        </product>
    </products>
    <productgroups>
        <productgroup>
            <id>1</id>
            <name>Juice</name>
        </productgroup>
    </productgroups>
</data>"""

def main():
    #Do a stack based iteration through all nodes in the tree
    tree = comet.xmltree.parse(data)
    stack = [(tree.getRoot(), 0)] #store node and depth as entries

    while stack:
        node, depth = stack.pop()
        name = node.getName()
        children = node.getChildren()
        while children:
            stack.append((children.pop(), depth + 1))

        comet.wlog('\t' * depth + name) #use the depth to indent the log message

    return 0