PHP creates XML document through DOM _ PHP Tutorial

Source: Internet
Author: User
PHP creates an XML document through DOM. Recently, I used the zendframework as a project. I want to take out the data in the database and generate an XML document. Record the process here: xml File: 1 .? Xmlversion1.0encodingUTF-8? 2. recently, the zendframework is used as a project. you need to extract the data in the database to generate an XML document. Record the process here:

Xml file:

1.

2.

3. Docker JCP1

4. Docker JCP2

5. Docker JCP3

6. Docker JCP4

7. Docker JCP5

8. Docker JCP6

9.

1. create testController. php and add function.

1. public function projectAction (){

1. // XML-related routine

2. // $ list refers to the data array in xml.

3. $ dom = new DOMDocument ('1. 0', 'utf-8 ');

4. // create root element

5. $ root = $ dom-> createElement ("data ");

6. $ dom-> appendChild ($ root );

7.

8. // project name xml list

9. foreach ($ list as $ project_item ){

10. // create child element

11. $ projectname = $ dom-> createElement ('Project ');

12. $ root-> appendChild ($ projectname );

13.

14. // create CDATA section

15. $ cdata = $ dom-> createCDATASection ($ project_item-> name );

16. $ projectname-> appendChild ($ cdata );

17.

18. $ id = $ dom-> createAttribute ("id ");

19. $ projectname-> appendChild ($ id );

20.

21. // create attribute value node

22. $ idValue = $ dom-> createTextNode ($ project_item-> project_id );

23. $ id-> appendChild ($ idValue );

24 .}

25. $ output = $ dom-> saveXML ();

26.

27. // Setting up headers and body

28. $ this-> _ response-> setHeader ('content-type', 'text/xml; charset = utf-8 ')

29.-> setBody ($ output );

30. $ this-> _ helper-> layout-> disableLayout ();

31 .}

In this way, you can output xml on the page.

To save the xml, use $ dom-> save ("filename. xml ");

This article is from the "Bob" blog

Bytes. Record the process here: xml File: 1 .? Xml version = 1.0 encoding = UTF-8? 2 ....

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.