Php xml data parsing code [json, parser function]

Source: Internet
Author: User
The code is as follows: Copy code

// Xml string
$ Xml_string = "<? Xml version = '1. 0'?>
<Users>
<User id = '000000'>
<Name> Foo </name>
<Email> foo@bar.com </name>
</User>
<User id = '000000'>
<Name> Foobar </name>
<Email> foobar@foo.com </name>
</User>
</Users> ";

// Load the xml string using simplexml
$ Xml = simplexml_load_string ($ xml_string );

// Loop through the each node of user
Foreach ($ xml-> user as $ user)
{
// Access attribute
Echo $ user ['id'], '';
// Subnodes are accessed by-> operator
Echo $ user-> name ,'';
Echo $ user-> email, '<br/> ';
}

Json data parsing code

 

The code is as follows: Copy code

$ Json_string = '{"id": 1, "name": "foo", "email": "foo@foobar.com", "interest": ["wordpress ", "php"]} ';
$ Obj = json_decode ($ json_string );
Echo $ obj-> name; // prints foo
Echo $ obj-> interest [1]; // prints php


// Xml string
$ Xml_string = "<? Xml version = '1. 0'?>
<Users>
<User id = '000000'>
<Name> Foo </name>
<Email> foo@bar.com </name>
</User>
<User id = '000000'>
<Name> Foobar </name>
<Email> foobar@foo.com </name>
</User>
</Users> ";

// Load the xml string using simplexml
$ Xml = simplexml_load_string ($ xml_string );

// Loop through the each node of user
Foreach ($ xml-> user as $ user)
{
// Access attribute
Echo $ user ['id'], '';
// Subnodes are accessed by-> operator
Echo $ user-> name ,'';
Echo $ user-> email, '<br/> ';
}

Php also comes with a php xml Parser
Introduction to php xml Parser
XML functions allow us to parse XML documents, but cannot be verified.

XML is a data format used for exchanging standard structured documents. You can find more information about XML in our XML tutorial.

This extension uses the Expat XML parser.

Expat is an event-based parser that treats XML documents as a series of events. When an event occurs, it calls a specified function to process it.

Expat is an unverified parser that ignores any DTD linked to the document. However, if the document format is not good, it will end with an error message.

Because it is event-based and does not validate, Expat is fast and suitable for web applications.

The XML parser function allows us to create an XML parser and define handles for XML Events.

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.