Problems with reading XML files

Previous topic - Next topic

apastuszak

I wrote this code to read an XML file:

#!/usr/bin/env python

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET

tree = ET.ElementTree(file='2012_Stamps.xml')
root = tree.getroot()

for subelement in root.getiterator():
for elements in subelement:
if elements.tag == "category":
category = elements.text
elif elements.tag == "name":
Title = elements.text
elif elements.tag == "fdoi":
FDOI = elements.text
elif elements.tag == "width":
stampwWidth = elements.text
elif elements.tag == "height":
stampHeight = elements.text
elif elements.tag == "description":
Description = elements.text
elif elements.tag == "image":
imageFile = elements.text
hOffSet = ((stampWidth * Multiplier) - (stampWidth * imageShrink)) / 2
vOffSet = ((stampHeight * Multiplier) - (stampHeight * imageShrink)) / 2


The code works fine from the command line, but when I try to run the code from within Scribus, none of my variables are defined.  Anyone know what I am doing wrong?

a.l.e

please also share a "dummy" xml file so that i can run the script and test it...

ciao
a.l.e

apastuszak

Here you go:

<?xml version="1.0" encoding="ISO-8859-1"?>

<stamplist>
<stamp>
<category>Commemorative</category>
<name>Arizona Statehood</name>
<fdoi>February 14, 2012</fdoi>
<width>39.6</width>
<height>25.2</height>
<description>One of America&apos;s last frontiers, Arizona joined the Union on February 14, 1912, becoming our 48th state. To commemorate the 100th anniversary of statehood, Cathedral Rock is pictured, one of the colorful sandstone rock formations of Sedona, Arizona. The design celebrates the &quot;Grand Canyon State&apos;s&quot; stunning natural beauty and abundant natural resources.</description>
<image>arizona.jpg</image>
</stamp>
</stamplist>


I got it working to some extent by declaring a ton of global variables, but there has got to be a better way to do it.

apastuszak

I have duplicated the problem using the standard python interpreter.  Iterating through an XML file doesn't behave quite like i expected it to, and trying to find code examples of exactly what I want to do has been next to impossible to find.  Hours of Googling has yielded almost nothing of use.  I'll take another whack at it tonight, if I can find some time.

apastuszak

Ok, I got some working code now!  It's rough.  Passing it along for others.  Once I comment it better, I'll post some examples, and all needed files.

#!/usr/bin/env python

'''
A script to help create stamp album pages
'''

from scribus import *

import xml.etree.ElementTree as ET

Multiplier = 1.3
imageShrink = 0.9
imagePath = "C:\\Users\\Andy\\Google Drive\\Scribus scripts\\"
height1 = 7
height2 = 4
height3 = 1.1
yCoor = 50
height4 = 20
category = "all"
Title = ""
FDOI = ""
stampWidth = 2.3
stampHeight = 1.2
Description = ""
imageFile = ""
hOffSet = 1.2
vOffSet = 1.2

tree = ET.ElementTree(file="2012_Stamps.xml")
root = tree.getroot()

for subelement in root.getiterator():
        if subelement.tag == "stamp":
            category = subelement[0].text
            Title = subelement[1].text
            FDOI = subelement[2].text
            stampWidth = subelement[3].text
            stampWidth = float(stampWidth)
            stampHeight = subelement[4].text
            stampHeight = float(stampHeight)
            Description = subelement[5].text
            imageFile = subelement[6].text
            hOffSet = ((stampWidth * Multiplier) - (stampWidth * imageShrink)) / 2
            vOffSet = ((stampHeight * Multiplier) - (stampHeight * imageShrink)) / 2
            height3 = (stampHeight* Multiplier)
            print category
            print Title
            print FDOI
            print stampWidth
            print stampHeight
            print Description
           
            T = createText(50, yCoor, stampWidth * Multiplier, height1)
            setText(Title, T)
            setFont("League Gothic Regular", T)
            setFontSize(16, T)
            setLineSpacing(16, T)
            setTextAlignment(ALIGN_CENTERED, T)
       
            D = createText (50, yCoor + height1, stampWidth * Multiplier, height2)
            setText(FDOI, D)
            setFont("Junction Regular", D)
            setFontSize(7, D)
            setLineSpacing(7, D)
            setTextAlignment(ALIGN_CENTERED, D)
       
            B = createText(50 + ((stampWidth * Multiplier) - (stampWidth * Multiplier) *0.9) / 2, yCoor + height1 + height2 + height3 + 4, (stampWidth * Multiplier) *0.9, 20)
            setText(Description, B)
            setFont("Junction Regular", B)
            setFontSize(6, B)
            setTextAlignment(3, B)
            setLineSpacing(6, B)
       
            C = createImage(50 + hOffSet, yCoor + height1 + height2 + 2 + vOffSet, stampWidth * imageShrink, stampHeight * imageShrink)
            loadImage(imagePath + imageFile, C)
            setScaleImageToFrame(1, 0, C)
       
            h = createRect(50, yCoor + height1 + height2 + 2, stampWidth * Multiplier, stampHeight * Multiplier)
       
            groupList=[C, h, T, B, D]
       
            groupObjects(groupList)
       


a.l.e


apastuszak

Don't you love it when users fix their own problems!   ;D