Parse XML documents with Python

Parse XML documents with Python

What is XML?

XML stands for eXtensible Markup Language.

XML is a markup language much like HTML.

XML was designed to store and transport small/medium data

About XML

XML works just like HTML, are start tags and end tags.

Tags indicate the beginning and ending of elements.

Attributes are key-value pairs on the start tag.

In XML it doesn’t matter extra spaces you put in.

<product>
    <id>2657</id>
    <name>Product name</name>
    <url/>
    <price>47.95</price>
    <brand>Calvin Klein</brand>
    <color>Black Velvet</color>
    <size available="L, XL, XXL"/>
  </product>

The Difference Between XML and HTML

XML was designed to carry data, with focus on what data is

while

HTML was designed to display data, with focus on how data looks

The XML language has no predefined tags, while HTML tags are.

What is Serialization / Deserialization

Serialization / Deserialization is the process of converting the data into a common format that can be stored/transmitted between systems in an independent matter.

What is XML schema?

An XML schema is used to define the structure of an XML document, describing what a given XML document can contain.

XML Validation is the way of verifying that the data is in the right format.

An XML schema is often used to specify a contract between systems. The contract itself is an XML document.

XSD Restrictions

Restrictions on Values

For an integer value

age element have to be integer.

<xs:element name="age">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
    </xs:restriction>
  </xs:simpleType>
</xs:element> 

Restrictions on a Set of Values

To limit the content of an XML element to a set of acceptable values, use the enumeration constraint.
The example below defines an element called “color” with a restriction. The only acceptable values are: red, blue, white:

<xs:element name="color">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="red"/>
      <xs:enumeration value="blue"/>
      <xs:enumeration value="white"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element> 

Question:

If you were building an XML Schema and wanted to limit the values allowed in an xs:string field to only those in a particular list, what XML tag would you use in your XML Schema definition?

xs:enumeration

Parse XML documents with Python

Python enables you to parse and modify the XML document. In order to parse the XML document, you need to have the entire XML document in memory.

How to use ElementTree to print nicely formatted XML documents?

fromstring() method parses XML from a string directly into an Element, which is the root element of the parsed tree.

findall() method – Find all matching subelements by tag name or path.

Parse XML with xml.etree.ElementTree Example

import xml.etree.ElementTree as et
data = '''<products>
    <product>
      <id>2657</id>
      <name>Women's Sleeveless Round Neck Fit and Flare Dress</name>
      <url></url>
      <price>47.95</price>
      <brand>Calvin Klein</brand>
      <color>Black Velvet</color>
      <size available="L, XL, XXL"/>
    </product>
    <product>
      <id>1343</id>
      <name>Womens Summer Dresses Soft Loose</name>
      <url></url>
      <price>45.05</price>
      <brand>Zara</brand>
      <color>Red</color>
      <size available="S, M, L, XL"/>
    </product>    
  </products>'''

products_tree = et.fromstring(data)
#products_tree is an Object, a tree of information

#Find all products, Get a list of objects, each object containing a product
products = products_tree.findall('product')
for product in products:
  #Get the name
  print('\nName:',product.find('name').text)
  
  #Get the color
  print('Color:',product.find('color').text)
  
  #Get available sizes
  print('Sizes:',product.find('size').get('available'))

//
Name: Women's Sleeveless Round Neck Fit and Flare Dress
Color: Black Velvet
Sizes: L, XL, XXL

Name: Womens Summer Dresses Soft Loose
Color: Red
Sizes: S, M, L, XL

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Reply

Your email address will not be published. Required fields are marked *