Seite 1 von 2

XML einlesen

Verfasst: Dienstag 4. Mai 2010, 11:54
von mit
Hallo,
ich habe folgende XML Datei:

Code: Alles auswählen

<Configuration>

        <APP>
                <Parameters>-x 6</Parameters>
        </APP>

        <Targets>
                <File Path="test/t">test1</File>
                <File Path="test/t">test2</File>
        </Targets>

        <Sources>
                <Source Name="B" Path="test/B">
                        <File MinValue="200" MaxValue="330">B002</File>
                        <File MinValue="200" MaxValue="320">B003</File>
                        <File MinValue="200" MaxValue="320">B004</File>
                </Source>
                <Source Name="E" Path="test/E">
                        <File MinValue="220" MaxValue="320">E001</File>
                </Source>
                <Source Name="A" Path="test/A">
                        <File MinValue="100" MaxValue="480">A001</File>
                        <File MinValue="1600" MaxValue="3800">A002</File>
                        <File MinValue="1600" MaxValue="3800">A003</File>
                </Source>
        </Sources>
</Configuration>
Ich möchte z.B. mit SAX die Datei einlesen und in eine geeignete Struktur speichern.
1.)
Wie würde eine geeignete Struktur aussehen um über alle Dateien (File) in Sources mit einer for-Schleife durchlaufen zu können und einen Zugang zur MinValue und MaxValue der aktuellen Dateien haben?

2.)
Analog zur 1 nur für Targets.

3.)
Wie könnte man den Path jeder Datei zuweisen z.B. test/B/B002?

Leider habe ich nicht der gleichen im Internet finden können.

Viele Grüße

Verfasst: Dienstag 4. Mai 2010, 13:00
von BlackJack
@mit: Wie soll denn die Datenstruktur aussehen, mit der Du gerne Arbeiten möchtest? Wenn Du die hast, brauchst Du nur noch Code schreiben der das XML in diese Struktur umwandelt.

Wie die Struktur die Du haben möchtest aussieht, kannst nur Du sagen, denn Du musst damit am Ende ja arbeiten.

Von SAX würde ich die Finger lassen und `lxml` verwenden. Mittels XPath kann man da ganz gut die Knoten beschreiben, die man gerne hätte.

Verfasst: Dienstag 4. Mai 2010, 13:13
von ms4py
Ich würde nicht die Standard-Lib verwenden sondern `lxml` für XML-Parsing. Die Dokumentation dazu ist auch ganz brauchbar.

Verfasst: Dienstag 4. Mai 2010, 13:38
von BlackJack

Verfasst: Dienstag 4. Mai 2010, 14:13
von mit
Lxml sieht gut aus und vielen dank BlackJack für dein Code. Ich werde es gleich ausprobieren.

Ich habe dies an meiner XML Datei geändert:

Code: Alles auswählen

        <Targets>
                <File Name="test1" Path="test/t">test1</File>
                <File Name="test2" Path="test/t">test2</File>
        </Targets>
Der folgende Pseudocode beschreibt wie die Information an ein Programm übergeben könnten oder gibt es einen bessere Lösung?

Code: Alles auswählen

for targetFile in Targets:
	print Name # z.B. test1
	tfile = TargetPath + TargetFile	# z.B. test/t/test1
	for Source in Sources:
		for sourceFile in Source:
			print Name # z.B. B
			sourceFile = sourcePath + sourceFile # z.b. test/B/B002
			cmd = "pragramm " + tfile + sourceFile + MinValue + MaxValue + APP.Parameters
			print cmd # z.B. programm test/t/test1 test/B/B002 200 330 -x 6

Verfasst: Dienstag 4. Mai 2010, 14:55
von BlackJack
@mit: Was ist der Name einer Datei, also das Attribut, im Unterschied zum Dateinamen, also dem Wert, von so einem `Targets/File`-Element?

Verfasst: Mittwoch 5. Mai 2010, 10:33
von mit
@BlackJack: Du hast recht, dass es nicht eindeutig ist. Der Wert ist der Dateiname und das Attribut eine kurze Beschreibung der Datei. Ich den xml Eintrag wie folgt geändert.

Code: Alles auswählen

        <Targets>

                <File Description="tes1" Path="test/t">AA2.txt</File>
                <File Description="tes2" Path="test/t">AA3.txt</File>

        </Targets>
Der folgende Pseudocode beschreibt wie die Information an ein Programm übergeben könnten oder gibt es einen bessere Lösung?

Code: Alles auswählen

for targetFile in Targets:
	print Description # z.B. test1
	tfile = TargetPath + TargetFile	# z.B. test/t/AA2.txt
	for Source in Sources:
		for sourceFile in Source:
			print Name # z.B. B
			sourceFile = sourcePath + sourceFile # z.b. test/B/B002
			cmd = "pragramm " + tfile + sourceFile + MinValue + MaxValue + APP.Parameters
			print cmd # z.B. programm test/t/AA2.txt test/B/B002 200 330 -x 6

Verfasst: Donnerstag 6. Mai 2010, 22:28
von bankkind
Ich habe mal eine Frage zu dem Thema. Wieso geht die Empfehlung von den meisten hier in Richtung lxml?

Verfasst: Donnerstag 6. Mai 2010, 22:36
von Hyperion
bankkind hat geschrieben:Ich habe mal eine Frage zu dem Thema. Wieso geht die Empfehlung von den meisten hier in Richtung lxml?
Weil es viele Features bietet (z.B. XPath & XSD-Validierung) und recht schnell ist.

Verfasst: Freitag 7. Mai 2010, 12:13
von Leonidas
Hyperion hat geschrieben:Weil es viele Features bietet (z.B. XPath & XSD-Validierung) und recht schnell ist.
Und gut dokumentiert ist, und sich so verhält wie man es erwartet, weils leicht zu installieren ist, gut maintained...

Verfasst: Freitag 7. Mai 2010, 14:14
von Dav1d
Ich finde lxml auch gut, allerdings reicht mir für 80% der "etree" aus der Std-Bibilothel [mod]xml.etree.elementtree[/mod] bzw. der cElementTree

Verfasst: Freitag 7. Mai 2010, 16:15
von Leonidas
Dav1d hat geschrieben:Ich finde lxml auch gut, allerdings reicht mir für 80% der "etree" aus der Std-Bibilothel [mod]xml.etree.elementtree[/mod] bzw. der cElementTree
Der scheitert schon an CSS/XPath, als das was ich in 90% aller Fälle nutzen will.

Re: XML einlesen

Verfasst: Samstag 15. Mai 2010, 13:44
von mit
@ BlackJack: Ich habe dein Code versucht zum laufen zu bringen, aber leider bekomme ich diesen Fehler:

Code: Alles auswählen

$python xml.py
Traceback (most recent call last):
  File "xml.py", line 5, in <module>
    from lxml import etree
ImportError: /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: symbol xmlSchematronSetValidStructuredErrors, version LIBXML2_2.6.32 not defined in file libxml2.so.2 with link time reference
Aber ich habe lxml installiert:

Code: Alles auswählen

$python
Python 2.6.4 (r264:75706, Jan 28 2010, 14:19:59) 
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
>>> 
Was habe ich falsch gemacht?

Re: XML einlesen

Verfasst: Samstag 15. Mai 2010, 15:43
von Leonidas
Das sieht nach einer kaputten Installation aus, wie hast du es denn installiert?

Re: XML einlesen

Verfasst: Samstag 15. Mai 2010, 22:53
von mit
Ich habe es wie folgt installiert.
---------
ftp://xmlsoft.org/libxml/

cd ~/temp/libxml2-2.7.7
$ configure --prefix=/home/mit/bin/libxml2
$ make
$ make install

$ cd /home/mit/programs/python/lib/python2.6/site-packages
$ ln -s /home/mit/bin/libxml2/lib/python2.6/site-packages/* .

----------
ftp://xmlsoft.org/libxslt/

$ ./configure --prefix=/home/mit/bin/libxslt --with-libxml-prefix=/home/mit/bin/libxml2

$ make
$ make install

$ cd /home/mit/programs/python/lib/python2.6/site-packages
$ ln -s /home/mit/bin/libxslt/lib/python2.6/site-packages/* .

---------
$ cd ~/temp/lxml-2.2.6/

$ python setup.py install --with-xml2-config=/home/mit/bin/libxml2/bin/xml2-config --with-xslt-config=/home/mit/bin/libxslt/bin/xslt-config

Wo könnte der Fehler sein?

Re: XML einlesen

Verfasst: Samstag 15. Mai 2010, 23:28
von Leonidas
Ich vermute mal er versucht die falsche libxml2.so.2 zu laden. Was gibt er aus wenn du ``ldd /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so`` eingibst?

Re: XML einlesen

Verfasst: Sonntag 16. Mai 2010, 05:38
von mit

Code: Alles auswählen

$ldd /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxslt.so.1: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxslt.so.1: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxslt.so.1: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxslt.so.1: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxslt.so.1: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxslt.so.1: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxslt.so.1: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
/home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: /usr/lib64/libxml2.so.2: no version information available (required by /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so)
	libxslt.so.1 => /usr/lib64/libxslt.so.1 (0x00002b6c90034000)
	libexslt.so.0 => /usr/lib64/libexslt.so.0 (0x00002b6c90269000)
	libxml2.so.2 => /usr/lib64/libxml2.so.2 (0x00002b6c9047b000)
	libz.so.1 => /usr/lib64/libz.so.1 (0x00002b6c907b9000)
	libm.so.6 => /lib64/libm.so.6 (0x00002b6c909cd000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b6c90c50000)
	libc.so.6 => /lib64/libc.so.6 (0x00002b6c90e6b000)
	libgcrypt.so.11 => /usr/lib64/libgcrypt.so.11 (0x00002b6c911bf000)
	libgpg-error.so.0 => /usr/lib64/libgpg-error.so.0 (0x00002b6c9140b000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00002b6c9160f000)
	/lib64/ld-linux-x86-64.so.2 (0x0000003501c00000)
	libnsl.so.1 => /lib64/libnsl.so.1 (0x00002b6c91813000)
Ich glaube du hast recht, aber leider weiss ich nicht wie man es beheben könnte.

Re: XML einlesen

Verfasst: Montag 17. Mai 2010, 18:00
von Snoda
kann es sein, dass der Dateiname im paket genutzt wird - also xml.py ?

Re: XML einlesen

Verfasst: Montag 17. Mai 2010, 21:49
von mit
Aber ich bekomme den Fehler mit nur den import Teil:

Code: Alles auswählen

$python
Python 2.6.4 (r264:75706, Jan 28 2010, 14:19:59) 
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import etree
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /home/mit/programs/python/lib/python2.6/site-packages/lxml-2.2.6-py2.6-linux-x86_64.egg/lxml/etree.so: symbol xmlSchematronSetValidStructuredErrors, version LIBXML2_2.6.32 not defined in file libxml2.so.2 with link time reference
>>> 
Was könnte der Fehler sein?

Re: XML einlesen

Verfasst: Dienstag 18. Mai 2010, 12:06
von mkesper
Ist lxml nicht in deiner Paketverwaltung verfügbar?