Quickly start Perl XML: Interface

xiaoxiao2021-03-06  41

Quickly start Perl XML: Interface

Translation: Fayland: China Perl Association FPC (Foundation of Perlchina) Original Name: Perl XML QuickStart: The Perl XML Interfaces Author: Kip Hampton Original: http://www.xml.com/pub/a/2001 / 04/18/erlxmlqStart1.html Table: April 18, 2001Perlchina Remind you: Please protect the author's copyright and maintain the crystallization of the author's labor.

Getting Started Introduction

Perl-XML mail groups often ask questions how to give unfamiliar users a quick guidance overview document on a large number of Perl XML modules. I will write a few column articles separately in the next few months.

The XML module on the CPAN can be divided into three categories: providing a unique interface for XML data (usually related to the conversion between XML instances and Perl data), implement a standard XML API module, and some specific XML related tasks Simplified special purpose modules. This month we pay attention to the first, XML Perl private interface.

Use disclaimer qw (: standard); This document is not based on the performance of the module performance, and my purpose is not implying that a module is more useful than another module. Choosing the right XML module for your project more dependent on the project itself and your accumulation experience. Different interfaces are adapted to different tasks and different people. My sole purpose is to display how the same final result is displayed by defining two simple tasks and then providing the travelable example of different excuses.

The task is very much in XML, but most of the XML-related tasks can be divided into two groups: First, extract data from existing XML documents, and the other is to create a new XML document using data using other resources. In this case, what we use to introduce different modules will consist of "extracting a specific dataset from an XML file" and "turning a perl data structure to a particular XML format".

Task 1: Extract data First, assume that the following XML piece:

DROMEDARY, OR Arabian Camel

300 to 690 kg.

THE DROMEDARY CAMEL IS Characterized by a long-curved

NECK, Deep-Narrow Chest, And A Single Hump.

...

The driveDary Camel is an Herbivore.

...

THE DROMEDARY CAMEL HAS a LifeSpan of About 40-50 Years

...

With The Exception of Rutting Males, DROMEDARIES SHOW

Very Little Aggressive Behavior.

...

The Camels Prefer Desert Conditions Characterized by a

Long Dry season and a short rainy season.

...

Since The Dromedary Camel Is Domesticated, The Camel HAS

No special status in conservation.

...

Now we assume that this full document (can be obtained from the example of this month) contains all the information of all members of the camel family, not just the above single peak camel information. To illustrate how each module extracts a subset of subset from this file, we will write a very short script to process the Camelids.xml document and the ordinary name of each class we found on Stdout (Common-Name) ), Latin name (packets with parentheses), and current preservation status. Therefore, handling the full document, the output of each script should be the following results:

Bactrian Camel (Camelus Bactrianus) endangered

DROMEDARY, ORABIAN CAMEL (Camelus DROMEDARIUS) No Special Status

Llama (lama glama) No Special Status

Guanaco (Lama Guanicoe) Special Concern

VICUNA (VICUGNA VICUGNA) EndanGred

Task 2: Creating an XML document To demonstrate how each module creates a new XML document from other data sources, we will write a small script to convert a simple Perl Hash to a simple XHTML document. Hash contains a URLS that pointing to a specific COOL-specific camel web page.

Hash as follows:

MY% Camelid_Links =

One => {url => '

http://www.online.discovery.com/news/picture/may99/photo20.html ',

Description => 'Bactrian Camel In Front of Great'.

'Pyramids in giza, egypt.'},

Two => {url => 'http://www.fotos-online.de/ENGLISH / M/09/9532.htm',

Description => 'DROMEDARY CAMEL ILLUSTRATES THE'.

'Importance of Accessorizing.'},

Three => {url => 'http://www.eskimo.com/~wallama/funny.htm',

Description => 'Charlie - Biography of a narcissistic Llama.'},

Four => {url => 'http://arrow.colorado.edu/travels/ther/turkey.html', intescription =>' a Visual Metaphor for the perl5-porters'.

'List?'},

FIVE => {URL => 'http://www.galaonline.org/pics.htm',

Description => 'Many Cool Alpacas.'},

Six => {url => 'http://www.thpf.de/suedamerikareise/galerie/vicunas.htm',

Description => 'Wild Vicunas in a Scenic landscape.'}

);

And what we expect from the documentation created from Hash is:

charlie -

Biography of a narcissistic Llama.

Bactrian

Camel In Front of Great Pyramids in Giza, Egypt.

DROMEDARY

Camel Illustrates the Importance of Accessorizing.

Many Cool Alpacas.

a Visual

METAPHOR for the perl5-porters list?

Wild

Vicunas in a scenic landscape.

Good indent XML result file (as shown above) is important for reading, but this good space treatment is not required by our case. What we are concerned is that the result document is a well-structured / Well-Formed and it correctly expresses data in Hash.

The task is defined, and the next is the example of the code example.

XML Perl Private Interface Example

XML :: Simple initially created XML :: Simple to simplify read and write XML format profiles, there is no additional abstract interface between the converted XML document and the Perl data structure. All elements and properties can be read directly by nested references.

Reading

Use xml :: simple;

MY $ file = 'files / camelids.xml'; MY $ XS1 = XML :: Simple-> New ();

MY $ DOC = $ XS1-> XMLIN ($ file);

Foreach my $ key (keys (% {$ doc -> {species}}) {

Print $ doc -> {species} -> {$ key} -> {'common-name'}. '('. $ key. ')';

Print $ DOC -> {species} -> {$ key} -> {conservation} -> final. "/ n";

}

Writing

Use xml :: simple;

Require "files / camelid_links.pl";

MY% Camelid_LINKS = GET_CAMELID_DATA ();

MY $ xsimple = Xml :: Simple-> new ();

Print $ xsimple-> xmlout (/% camelid_links,

Noattr => 1,

Xmldecl => '

');

The condition of this data to the document is required to expose an weak point of XML :: Simple: It does not allow us to decide which key in the haze should return and which key is returned as the property. Although the output of the above example is close to our output requirements but is far from enough. For those who prefer to use the XML document content directly as Perl data structure, and need to make a finer control in the output, XML :: Simple and XML :: Writer works well.

The following example explains how to use XML :: Write to meet our output requirements.

Use xml :: writer;

Require "files / camelid_links.pl";

MY% Camelid_LINKS = GET_CAMELID_DATA ();

MY $ Writer = XML :: Writer-> New ();

$ Writer-> Xmldecl ();

$ Writer-> StartTag ('html');

$ Writer-> StartTag ('Body');

Foreach my $ item (keys (% Camelid_Links) {

$ Writer-> StartTag ('a', 'href' => $ camelid_links {$ item} -> {url});

$ Writer-> Characters ($ Camelid_Links {$ Item} -> {Description});

$ Writer-> Endtag ('a');

}

$ Writer-> Endtag ('Body');

$ Writer-> endtag ('html');

$ Writer-> End ();

XML :: SimpleObjectXML :: SimpleObject The accessor / accessor of the XML Data Review Document Object Model provides a face-to-object interface.

Reading

Use xml :: parser;

Use xml :: simpleObject;

MY $ file = 'files / camelids.xml';

MY $ PARSER = XML :: Parser-> New (ErrorContext => 2, Style => "Tree");

MY $ XSO = XML :: SimpleObject-> New ($ PARSER-> PARSEFILE ($ file); Foreach MY $ species ($ XSO-> Child ('Camelids') -> Children ('Species')) {

Print $ species-> Child ('common-name') -> {value};

Print '('. $ species-> attribute ('name'). ')';

Print $ species-> Child ('Conservation') -> Attribute ('status');

Print "/ n";

}

WritingXML :: SimpleObject does not create the functionality of the XML document via grabbing. But as in the XML :: Simple example above, you can use the XML :: Writer to fit the simple completion task.

XML :: Treebuilderxml :: Treebuilder package consists of two modules: XML :: Element Used to create and get XML element points and xml :: Treebuilder as a factory package to simplify the document tree from an existing XML file. For those who have a respectable HTML :: Element and HTML :: Tree modules, using XML :: Treebuilder is very easy, because others are the same except for XML unique methods.

Reading

Use xml :: Treebuilder;

MY $ file = 'files / camelids.xml';

My $ tree = xml :: Treebuilder-> new ();

$ tree-> parse_file ($ file);

Foreach MY $ species ($ TREE-> FIND_BY_TAG_NAME ('Species')) {

Print $ species-> Find_BY_TAG_NAME ('Common-Name') -> as_Text;

Print '('. $ species-> attr_get_i ('name'). ')';

Print $ species-> Find_BY_TAG_NAME ('Conservation') -> attr_get_i ('status');

Print "/ n";

}

Writing

Use xml :: element;

Require "files / camelid_links.pl";

MY% Camelid_LINKS = GET_CAMELID_DATA ();

MY $ root = Xml :: Element-> New ('html');

MY $ body = Xml :: Element-> New ('body');

MY $ XML_PI = XML :: Element-> New ('~ pi', text => 'XML Version = "1.0"');

$ root-> push_content ($ body);

Foreach my $ item (keys (% Camelid_Links) {

MY $ link = xml :: Element-> New ('a', 'href' => $ code} -> {url}); $ link-> push_content ($ Camelid_Links {$ Item} -> {Description });

$ body-> push_content ($ link);

}

Print $ xml_pi-> as_XML;

Print $ root-> as_xml ();

XML :: Twigxml :: Twig differs from other XML interfaces with Perl, which is an PerLish interface that is rich in creative features except for the standard XML API. For more detailed introductions, please see

XML.com Article

Reading

Use xml :: twig;

MY $ file = 'files / camelids.xml';

MY $ twig = Xml :: twig-> new ();

$ twig-> parsefile ($ file);

MY $ root = $ twig-> root;

Foreach MY $ Species ($ root-> children ('species')) {

Print $ species-> first_child_text ('common-name');

Print '('. $ species-> att ('name'). ')';

Print $ species-> first_child ('consservation') -> ATT ('status');

Print "/ n";

}

Writing

Use xml :: twig;

Require "files / camelid_links.pl";

MY% Camelid_LINKS = GET_CAMELID_DATA ();

MY $ root = xml :: twig :: ELT-> New ('html');

MY $ body = xml :: twig :: ELT-> New ('body');

$ Body-> Paste ($ root);

Foreach my $ item (keys (% Camelid_Links) {

MY $ link = xml :: twig :: ELT-> New ('a');

$ link-> set_att ('href', $ Camelid_Links {$ Item} -> {url});

$ link-> set_text ($ camelid_links {$ item} -> {description});

$ link-> paste ('last_child', $ body);

}

PRINT QQ | |

$ root-> print;

These examples illustrate the basic method of use of these ordinary XML Perl modules. My goal is to provide enough example to let you feel how to write code for each module. Next month we will pay attention to "Module to implement a standard XML API", specifically explained, XML :: DOM, XML :: XPath and other large SAX and class SAX modules.

Resources

Download Case Code CPN's full list of XML modules Perl-xml mail group file Using XML :: Twig Second Part: Perl XML QuickStart: The Standard XML Interfaces

转载请注明原文地址:https://www.9cbs.com/read-68091.html

New Post(0)