| 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.