Four ways to generate xml files in php

WBOY
Release: 2016-07-25 08:59:07
Original
1152 people have browsed it
  1. title1
  2. content1
  3. 2009-10-11
  4. title2</title> ;</li> <li> <content>content2</content></li> <li> <pubdate>2009-11-11</pubdate></li> <li> </item></li> <li></article></li> </ol></div> <em onclick="copycode($('code_CAg'));">Copy code</em> </div> <p>Method 1, generate directly string Use pure PHP code to generate a string and write the string to a file with an XML suffix. </p> <div class="blockcode"> <div id="code_O2x"><ol> <li> <li> <li><p><?PHP<li>$data_array = array(<li> array(<li> 'title' => 'title1',</li> <li> 'content' => 'content1',</li> <li> 'pubdate' => '2009-10-11',</li> <li> ),</li> <li> array(</li> <li> 'title' => 'title2',</li> <li> 'content' => 'content2',</li> <li> 'pubdate' => '2009-11 -11',</li> <li> )</li> <li>);</li> <li>$title_size = 1;</p></li> <li><p>$xml = "<?xml version="1.0" encoding="utf-8"?>n" ;</li> <li>$xml .= "<article>n";</p></li> <li><p>foreach ($data_array as $data) {</li> <li>$xml .= create_item($data['title'], $title_size , $data['content'], $data['pubdate']);</li> <li>}</p></li> <li><p>$xml .= "</article>n";</li> <li>echo $xml;< /p></li> <li><p>//Create XML single item</li> <li>function create_item($title_data, $title_size, $content_data, $pubdate_data)</li> <li>{</li> <li> $item = "<item>n";</li> <li> $item .= "< ;title size="" . $title_size . "">" . $title_data . "n";
  5. $item .= "" . $content_data . "n ";
  6. $item .= " " . $pubdate_data . "n";
  7. $item .= "n";

  8. return $item;

  9. }
  10. ?>

Copy code

Method 2, use DomDocument to generate XML files Steps: 1. Create nodes using the createElement method. 2. Create text content using the createTextNode method, 3. Add child nodes using the appendChild method. 4. Create attributes using the createAttribute method

  1. $data_array = array(

  2. array(
  3. 'title' => 'title1',
  4. 'content' => 'content1',
  5. 'pubdate' => '2009-10-11',
  6. ),
  7. array(
  8. 'title' => 'title2',
  9. 'content' => 'content2',
  10. 'pubdate' => '2009-11 -11',
  11. )
  12. );

  13. // Attribute array

  14. $attribute_array = array(
  15. 'title' => array(
  16. 'size' => 1
  17. )
  18. ) ;

  19. // Create an XML document and set the XML version and encoding. .

  20. $dom=new DomDocument('1.0', 'utf-8');

  21. // Create root node

  22. $article = $dom->createElement('article');
  23. $dom->appendchild($article);

  24. foreach ($data_array as $data) {

  25. $item = $dom->createElement('item');
  26. $article- >appendchild($item);
  27. create_item($dom, $item, $data, $attribute_array);
  28. }
  29. echo $dom->saveXML();

  30. function create_item( $dom, $item, $data, $attribute) {

  31. if (is_array($data)) {
  32. foreach ($data as $key => $val) {
  33. // Create element
  34. $$key = $dom ->createElement($key);
  35. $item->appendchild($$key);

  36. // Create element value

  37. $text = $dom->createTextNode($val );
  38. $$key->appendchild($text);

  39. if (isset($attribute[$key])) {

  40. // If there are related attributes in this field, they need to be set
  41. foreach ($attribute[$key] as $akey => $row) {
  42. // Create attribute node
  43. $$akey = $dom->createAttribute($akey);
  44. $$key->appendchild($ $akey);

  45. // Create attribute value node

  46. $aval = $dom->createTextNode($row);
  47. $$akey->appendChild($aval);
  48. }
  49. } // end if
  50. }
  51. } // end if
  52. } // end function
  53. ?>

Copy code

Method 3, use XMLWriter class to create XML file1 2 Next Last Page



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template