当前位置:Gxlcms > PHP教程 > php生成xml和转换[源码]_PHP教程

php生成xml和转换[源码]_PHP教程

时间:2021-07-01 10:21:17 帮助过:20人阅读

  php生成xml就像树一样,逐个添加节点,可以在一个父节点下添加多个子节点, function madexml()

  1. {
  2. //获取模板信息
  3. $strTempInfo = $this->modelCmsObj->getTemplate(2007);
  4. $arrTemp = explode("#",$strTempInfo);
  5. array_shift($arrTemp);
  6. $arrContents = array();
  7. foreach($arrTemp as $k=>$v)
  8. {
  9. $arrContents[$k]=explode(",",$v);
  10. }
  11. //解析为xml文件
  12. $objDom = new DOMDocument("1.0");
  13. header("Content-Type: text/plain");
  14. //添加元素和文本节点
  15. $root = $objDom->createElement("recommend");
  16. $objDom->appendChild($root);
  17. foreach($arrContents as $k=>$v)
  18. {
  19. $item = $objDom->createElement("entry");
  20. $root->appendChild($item);
  21. $nextitem1 = $objDom->createElement("simgurl");
  22. $nextitem2 = $objDom->createElement("imgurl");
  23. $nextitem3 = $objDom->createElement("fileurl");
  24. $item->appendChild($nextitem1);
  25. $item->appendChild($nextitem2);
  26. $item->appendChild($nextitem3);
  27. $text1 = $objDom->createTextNode($v[0]);
  28. $text = $objDom->createTextNode($v[1]);
  29. $text2 = $objDom->createTextNode($v[2]);
  30. $nextitem2->appendChild($text1);
  31. $nextitem3->appendChild($text2);
  32. $nextitem1->appendChild($text);
  33. }
  34. echo $objDom->saveXML();
  35. }

simplexml_load_string把xml串转化为字符串

  1. $string = <<//声明xml文档
  2. 1.0?>
  3. Forty What?
  4. Joe
  5. Jane
  6. I know thats the answer -- but whats the question?
  7. XML;
  8. $xml = simplexml_load_string($string);
  9. var_dump($xml);
  10. ?>
  11. This script will display:

    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/486093.htmlTechArticlephp生成xml就像树一样,逐个添加节点,可以在一个父节点下添加多个子节点, function madexml() { //获取模板信息 $strTempInfo = $this -modelCmsObj-g...

人气教程排行