Home  >  Article  >  Backend Development  >  Detailed explanation of three methods of outputting XML to page content in PHP

Detailed explanation of three methods of outputting XML to page content in PHP

WBOY
WBOYOriginal
2016-07-25 08:59:15862browse
  1. header("Content-type: text/xml");
  2. echo "";
  3. echo "";
  4. echo "";
  5. echo "";
  6. echo "小小菜鸟";
  7. echo "";
  8. echo "";
  9. echo "24";
  10. echo "";
  11. echo "";
  12. echo "男";
  13. echo "";
  14. echo "";
  15. echo "";
  16. echo "";
  17. echo "艳艳";
  18. echo "";
  19. echo "";
  20. echo "23";
  21. echo "";
  22. echo "";
  23. echo "女";
  24. echo "";
  25. echo "";
  26. echo "";
  27. ?>
复制代码

方法2,

  1. header("Content-type: text/xml");
  2. echo "";
  3. echo "小小菜鸟24艳艳23";
  4. ?>
复制代码

方法3,

  1. /*

  2. 用PHP的DOM控件来创建XML输出
  3. 设置输出内容的类型为xml
  4. edit bbs.it-home.org
  5. */
  6. header('Content-Type: text/xml;');
  7. //创建新的xml文件
  8. $dom = new DOMDocument('1.0', 'utf-8');

  9. //建立元素

  10. $response = $dom->createElement('response');
  11. $dom->appendChild($response);

  12. //建立元素并将其作为的子元素

  13. $books = $dom->createElement('books');
  14. $response->appendChild($books);

  15. //为book创建标题

  16. $title = $dom->createElement('title');
  17. $titleText = $dom->createTextNode('PHP与AJAX');
  18. $title->appendChild($titleText);

  19. //为book创建isbn元素

  20. $isbn = $dom->createElement('isbn');
  21. $isbnText = $dom->createTextNode('1-21258986');
  22. $isbn->appendChild($isbnText);

  23. //创建book元素

  24. $book = $dom->createElement('book');
  25. $book->appendChild($title);
  26. $book->appendChild($isbn);

  27. //将作为子元素

  28. $books->appendChild($book);

  29. //在一字符串变量中建立XML结构

  30. $xmlString = $dom->saveXML();

  31. //输出XML字符串

  32. echo $xmlString;
  33. ?>

复制代码


Statement:
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