Use of Perl to parse XML, JSON files, and curl tools

Source: Internet
Author: User

1. parse XML

 

Two packages are used:

 

# Use module
Use XML: simple;
Use Data: dumper;

 

 

Process:

 

# Create object
$ Xml = New XML: simple;

# Parsing

$ DATA = $ XML-> xmlin (XML file name );

# Value

My $ value = $ data-> {node name}

If a subnode exists

 

My $ value = $ data-> {node name}-> {child node name };

 

2. parse JSON

 

Perl package used

Use JSON;
Use Data: dumper;

 

JSCo data to be parsed

# My $ curl_result = '{"type": 1, "data": 2 }';

$ Hash = decode_json ($ curl_result );
$ Type = $ hash-> {'type'}. "/N ";
$ DATA = $ hash-> {'data'}. "/N ";

 

The value of type and data is parsed...

 

 

3. Perl calls the curl Library

 

Use www: curl: easy;

My $ res = $ curl-> setopt (curlopt_url, www.baidu.com );

 

Open (my $ fileb, ">"/home/someone/file );

$ Curl-> setopt (curlopt_writedata, $ fileb );

My $ retcode = $ curl-> perform;

Close ($ fileb );

 

 

 

 

 

 

 

 

 

 

Related Article

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.