Inhaltsverzeichnis

XML

Initialisierung

from bs4 import BeautifulSoup

Importiert die Klasse

soup = BeautifulSoup(daten, Parsername)

Instanziert ein BeatifulSoup-Objekt.

Beispiel:

daten = '<?xml version="1.0" encoding="utf-8"?> <SnapshotDeviceResponse xmlns="http://www.ecma-international.org/standards/ecma-323/csta/ed5">  <crossRefIDorSnapshotData> <snapshotData> <snapshotDeviceResponseInfo> <connectionIdentifier> <callID>9</callID> <deviceID>sip:482735035@172.20.1.7</deviceID> </connectionIdentifier> <localCallState> <compoundCallState> <localConnectionState>connected</localConnectionState> </compoundCallState> </localCallState> </snapshotDeviceResponseInfo> </snapshotData> </crossRefIDorSnapshotData> </SnapshotDeviceResponse>'

soup = BeautifulSoup(daten, 'xml')

soup repräsentiert anschließend den Tree.

Elementzugriff

Um ein Element im Baum anzusprechen:

soup.Element.Unterelement.Unterunterelement

Beispiel:

soup.crossRefIDorSnapshotData.snapshotData.snapshotDeviceResponseInfo.connectionIdentifier.callID

String-Zugriff

soup.Element.Unterelement.Unterunterelement.string

Beispiel:

soup.crossRefIDorSnapshotData.snapshotData.snapshotDeviceResponseInfo.connectionIdentifier.callID.string