Commit 0c13d110f572303bc10fe01bfb8429111166c4d4

Authored by Jürgen Knödlseder
1 parent 7a377ccd

Add XML example script.

Showing 1 changed file with 239 additions and 0 deletions
examples/python/xml_create.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# Scope
  4 +#
  5 +# This script provides an example for creating and handling XML documents
  6 +# with GammaLib
  7 +#
  8 +# Usage
  9 +# ./xml_create.py
  10 +#
  11 +# -------------------------------------------------------------------------
  12 +#
  13 +# Copyright (C) 2012 Juergen Knoedlseder
  14 +#
  15 +# This program is free software: you can redistribute it and/or modify
  16 +# it under the terms of the GNU General Public License as published by
  17 +# the Free Software Foundation, either version 3 of the License, or
  18 +# (at your option) any later version.
  19 +#
  20 +# This program is distributed in the hope that it will be useful,
  21 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  22 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23 +# GNU General Public License for more details.
  24 +#
  25 +# You should have received a copy of the GNU General Public License
  26 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  27 +#
  28 +# ==========================================================================
  29 +from gammalib import *
  30 +from math import *
  31 +
  32 +
  33 +# =================== #
  34 +# Create XML document #
  35 +# =================== #
  36 +def create_xml():
  37 + """
  38 + Create XML document example.
  39 +
  40 + This function creates a simple XML document that illustrates how an
  41 + XML document can be constructed with GammaLib. In this example, the
  42 + document creation is done linearly, in the order in which the nodes
  43 + are written into the document.
  44 +
  45 + The append() method is used to append nodes to the XML document root
  46 + and to child nodes. You will see two flavours of the append() method:
  47 + - one that takes a text string as argument, which serves to define
  48 + the name and the attribtes of the child element
  49 + - one that takes a child node as argument, which serves to append
  50 + either a text leaf using the GXmlText constructor, or other markup
  51 + nodes, such as processing instructions (GXmlPI) or comments
  52 + (GXmlComment)
  53 + """
  54 + # Allocate XML document
  55 + xml = GXml()
  56 +
  57 + # Append first child element to XML document root. The first child
  58 + # element contains a single child which is an empty-element tag.
  59 + level1 = xml.append('element type="Measurement"')
  60 + level2 = level1.append('parameter name="Flux" value="1.0"')
  61 +
  62 + # Append second child element to XML document root. The second child
  63 + # element contains a single child named list, which contains two
  64 + # childs that each contain a text leaf.
  65 + level1 = xml.append('element')
  66 + level2 = level1.append('list')
  67 + level3 = level2.append('string')
  68 + level4 = level3.append(GXmlText("This is a text"))
  69 + level3 = level2.append('integer')
  70 + level4 = level3.append(GXmlText("17"))
  71 +
  72 + # Append third child element to XML document root. The third child
  73 + # element contains a single Processing Instruction.
  74 + level1 = xml.append('processing-instruction')
  75 + level2 = level1.append(GXmlPI('<?xml-stylesheet type="text/xsl"?>'))
  76 +
  77 + # Append forth child element to XML document root. The forth child
  78 + # element contains two comments. The first comment is a single-line
  79 + # comment while the second comment runs over multiple lines.
  80 + level1 = xml.append('comment')
  81 + level2 = level1.append(GXmlComment('This is a comment'))
  82 + level2 = level1.append(GXmlComment('This is a\ntwo line comment'))
  83 +
  84 + # Return XML document
  85 + return xml
  86 +
  87 +
  88 +# ======================= #
  89 +# Manipulate XML document #
  90 +# ======================= #
  91 +def manipulate_xml(xml):
  92 + """
  93 + Example for manipulating an XML document.
  94 +
  95 + This function illustrates the manipulation of an XML document by
  96 + using the remove(), insert(), set() and extend() methods.
  97 + """
  98 + # Remove the third child node containing the processing instruction
  99 + # (Note that the third node has the index 2!).
  100 + xml.remove(2)
  101 +
  102 + # Insert now at the location where we just removed the processing
  103 + # instruction a new child element named 'replacement'. This new
  104 + # child element will have four levels of child nodes.
  105 + level0 = xml.insert(2, GXmlElement('replacement'))
  106 + level1 = level0.append('level1')
  107 + level2 = level1.append('level2')
  108 + level3 = level2.append('level3')
  109 + level4 = level3.append('level4')
  110 +
  111 + # Set now a text node at the place of the level4 child node (the
  112 + # level4 child node was sitting in the first slot of the level3
  113 + # node, hence we have to set the text using the index 0)
  114 + level4 = level3.set(0, GXmlText('This text replaces the level 4 child node'))
  115 +
  116 + # We extend the level2 child node by adding four further child nodes
  117 + # containg a text node, one comment and one processing instruction.
  118 + # In total, the level2 child node now has six childs, four of which
  119 + # are elements, one is a comment and one a processing instruction.
  120 + #
  121 + # We could do this simply using the append method, but we illustrate
  122 + # here another case: the extension of a child node using child nodes
  123 + # of another element. For this purpose we create first a fresh element
  124 + # 'ext'. To this fresh element we append four child nodes which all
  125 + # will contain a text node. We furthermore append one comment and one
  126 + # processing instruction. Once this is setup we call the extend() method
  127 + # to extend the level2 child node.
  128 + ext = GXmlElement('level2 extension="yes"')
  129 + element1 = ext.append('level3')
  130 + element2 = ext.append('level3')
  131 + element3 = ext.append('level3')
  132 + element4 = ext.append('level3')
  133 + comment = ext.append(GXmlComment('This is a comment'))
  134 + pi = ext.append(GXmlPI('<?xml-stylesheet type="text/xsl"?>'))
  135 + element1.append(GXmlText('This is the first element'))
  136 + element2.append(GXmlText('This is the second element'))
  137 + element3.append(GXmlText('This is the third element'))
  138 + element4.append(GXmlText('This is the forth element'))
  139 + level2.extend(ext)
  140 +
  141 + # Now we replace the text of the last element in the level2 childs.
  142 + # The emphasis is here on 'element'. The comment and the processing
  143 + # instruction are NOT elements, they are special markup tags. The
  144 + # last 'element' is the forth child node!
  145 + #
  146 + # Instead of explicitly quoting the index of the forth child, we
  147 + # determine the number of elements using the elements() method, and
  148 + # we access the last element by using number-1 as the index in the
  149 + # element() method.
  150 + #
  151 + # The text child is then accessed using the [0] index operator. As
  152 + # there is only a single text node in the last element, we use the
  153 + # index 0.
  154 + number = level2.elements()
  155 + last = level2.element(number-1)
  156 + text = last[0]
  157 + text.text('This replaces the text of the last element')
  158 +
  159 + # Now we change the name of the last element in the level2 childs
  160 + # to 'new3' and we benefit to illustrate also how attributes can
  161 + # be set. The name change is done using the name() method, attributes
  162 + # are manipulated using the attribute() method.
  163 + #
  164 + # There are three calls to attribute(). The first adds a new attribute
  165 + # 'type' with a value of 'none'. As this attribute did not exist before,
  166 + # the attribute() method adds it to the element. The same is done by the
  167 + # second call which add the attribute 'case' with value 'mixed'. The
  168 + # third class modifies the attribute 'type' by giving it a new value of
  169 + # 'info'. This illustrates the logic of the attribute() method: if the
  170 + # attribute name does not exist the attribute will be added, otherwise
  171 + # it will be modified.
  172 + last.name('new3')
  173 + last.attribute('type', 'none')
  174 + last.attribute('case', 'mixed')
  175 + last.attribute('type', 'info')
  176 +
  177 + # Now we make again a text replacement, but instead of chaning the
  178 + # text of the last element we change the text of the last element with
  179 + # name 'level3'. This is not the last in the list as we just changed
  180 + # the name of the last element to 'new3'. This example illustrates
  181 + # how elements can be accessed by name.
  182 + number = level2.elements('level3')
  183 + text = level2.element('level3', number-1)[0]
  184 + text.text('This replaces the text of the last "level3" element')
  185 +
  186 + # Return XML document
  187 + return xml
  188 +
  189 +
  190 +# ================= #
  191 +# Show XML document #
  192 +# ================= #
  193 +def show_xml(xml):
  194 + """
  195 + Show XML document on the screen.
  196 + """
  197 + # Allocate string URL
  198 + url = GUrlString()
  199 +
  200 + # Write XML document in URL
  201 + xml.write(url)
  202 +
  203 + # Print URL buffer
  204 + print(url.string())
  205 +
  206 + # Return
  207 + return
  208 +
  209 +
  210 +# ======================== #
  211 +# Main routine entry point #
  212 +# ======================== #
  213 +if __name__ == '__main__':
  214 + """
  215 + Simulate photons.
  216 + """
  217 + # Dump header
  218 + print("")
  219 + print("*************************************")
  220 + print("* Example for XML document handling *")
  221 + print("*************************************")
  222 + print("")
  223 +
  224 + # Create XML document
  225 + xml = create_xml()
  226 +
  227 + # Show XML document
  228 + print("1. create_xml() output")
  229 + print("======================")
  230 + show_xml(xml)
  231 +
  232 + # Manipulate XML document
  233 + xml = manipulate_xml(xml)
  234 +
  235 + # Show XML document
  236 + print("2. manipulate_xml() output")
  237 + print("==========================")
  238 + show_xml(xml)
  239 +
0 240 \ No newline at end of file
... ...