Build and parse data in XML format using native Ajax and PHP

Source: Internet
Author: User

Xml:extensible Markup Language, an extensible label language, is itself a string format used to describe bulk composite data,

Grammatical features:

(1) All data is placed in the label

(2) The entire XML string has and can have only one root tag

(3) All label names can be customized, but are strictly case-sensitive, and the start and end tags must be exactly the same

(4) Each label can be custom attributes, the attribute must have a value, the value must be enclosed in single quotation marks/double quotation marks.

(5) Each label can be defined as any sub-label, the label can be nested, but not cross

Note: The difference between HTML and XML

HTML syntax is arbitrary; XML syntax is strict;

HTML tags are predefined; XML tags are custom-defined;

HTML is used to describe the structure of a webpage; XML tags are used to describe data;

Here's an XML that we need to carry data, and then we're going to build XML-formatted data through PHP.

<?xml version= "1.0" encoding= "UTF-8"?> <productlist><product pid= "101" ><pname> Samsung </pname ><price>35.50</price><pic>1.jpg</pic></product><product pid= "102" >< pname> SanDisk </pname><price>45.50</price><pic>2.jpg</pic></product>< Product pid= "103" ><pname> Kingston </pname><price>30.50</price><pic>3.jpg</pic> </product></productlist>

  

PHP constructs XML structure data and responds

<?phpheader (' Content-type:application/xml '); $conn = Mysqli_connect ("127.0.0.1", "Root", "" "," Dangdang ", 3306); $ sql = "SET NAMES UTF8"; Mysqli_query ($conn, $sql); $sql = "SELECT * from book"; $result = Mysqli_query ($conn, $sql); $list = Mys Qli_fetch_all ($result, MYSQLI_ASSOC); $str = "<?xml version= ' 1.0 ' encoding= ' UTF-8 '?>"; $str. = "<productlist > "; foreach ($list as $b) {$str. =" <product pid= ' $b [bid] ' ><pname> $b [bname]</pname><price>$ b[price]</price><pic> $b [pic]</pic></product> ";} $str. = "</productlist>"; Echo $str;

  

Receive XML and parse because XML belongs to the Xmldom object can only be parsed using the core DOM method

<!doctype html>

  

Build and parse data in XML format using native Ajax and PHP

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.