Python3 XML parsing

Source: Internet
Author: User

What is XML?

XML refers to Extensible Markup Language (eXtensible Markup Language), a subset of the standard generic markup language, which is a markup language used to tag electronic files to make them structural. You can learn XML tutorials through this site

XML is designed to transmit and store data.

XML is a set of rules that define semantic markup that divides a document into parts and identifies the parts.

It is also a meta-markup language that defines syntactic languages used to define other, semantic, structured markup languages that are relevant to a particular domain.

Python parsing of XML

The common XML programming interface has DOM and sax, and the two interfaces handle XML files in different ways, of course, using different scenarios.

Python has three ways to parse Xml,sax,dom, and ElementTree:

1.SAX (Simple API for XML)

The Python standard library contains the SAX parser, which uses the event-driven model to process XML files by triggering events and invoking user-defined callback functions during parsing of XML.

2.DOM (Document Object Model)

Parses XML data into a tree in memory, manipulating the XML by manipulating the tree.

The XML instance file used in this section movies.xml the following:

<collection shelf= "New arrivals" ><movie title= "Enemy Behind" > <type>war, thriller</type> < format>dvd</format> <year>2003</year> <rating>PG</rating> <stars>10</ Stars> <description>talk about a us-japan war</description></movie><movie title= " Transformers "> <type>anime, Science fiction</type> <format>DVD</format> <year>1989 </year> <rating>R</rating> <stars>8</stars> <description>a schientific fiction& lt;/description></movie> <movie title= "Trigun" > <type>anime, action</type> <format&gt ;D vd</format> <episodes>4</episodes> <rating>PG</rating> <stars>10</stars&   Gt <description>vash the Stampede!</description></movie><movie title= "Ishtar" > <type> Comedy</type> <format>vhs</format> <rating>PG</rating> <stars>2</stars> <description>viewable boredom</description ></movie></collection>
Python parsing xml using sax

Sax is an event-driven API.

Parsing an XML document with sax involves two parts: the parser and the event handler.

The parser is responsible for reading the XML document and sending events to the event handler, such as the element starting with the element end event;

The event handler is responsible for the event, processing the passed XML data.

Python3 XML parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.