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