XML (How to get an XML file in PHP/ajax how to get the response data in XML format)

Source: Internet
Author: User
Tags vars

1. XML Format specification: ① must have a root element ② cannot have spaces, cannot be in numbers or. Start, case sensitive ③ non-overlapping nesting ④ Attribute double quotation marks (the browser is automatically corrected into double quotes) ⑤ Special symbols to use entities ⑥ annotations are the same as HTML Although complex data can be described and transmitted, its resolution is too complex and large, so implementation is rarely used.  

Cases:

  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <root>
  3. <arrayList>
  4. <array>
  5. <src>images/banner.jpg</src>
  6. <newpirce>12.00</newpirce>
  7. <oldprice>30.00</oldprice>
  8. </Array>
  9. <array>
  10. <src>images/banner.jpg</src>
  11. <newpirce>12.00</newpirce>
  12. <oldprice>30.00</oldprice>
  13. </Array>
  14. </arrayList>
  15. </root>

2. How to get the XML file in PHP: ①header to write Text/xml. file_get_contents 获取文件内容This has been removed in over 5.4 versions. So you want to modify the file_get_contents ("Php://input") to get the value of the XML.
    1. <?php
    2. Header (' Content-type:text/xml;charset=utf-8 ');
    3. / * Request Response content format when transferring data in XML format is text/xml*/
    4. /*file_get_contents Get File contents * /
    5. $xml = file_get_contents (' 01.xml ');
    6. / * Output XML content * /
    7. echo $xml;
    8. ?>



3. Ajax How to get the response data in XML format: Responsexml  
    1. var xhr = new XMLHttpRequest;
    2. Xhr.open (' get ',' 01.php ');
    3. Xhr.send (null);
    4. Xhr.onreadystatechange = function () {
    5. if (xhr.status = = && Xhr.readystate = = 4) {
    6. / * Gets the content returned to the XML format as a DOM object document*/
    7. var xml = Xhr.responsexml;
    8. / * You can get the data to XML via the selector * /
    9. Console.log (Xml.queryselectorall (' array ') [0].queryselector (' src '). InnerHTML);
    10. }
    11. }

XML (How to get an XML file in PHP/ajax how to get the response data in XML format)

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.