在编写xml文件时,无法在php中的xml节点中添加换行符

时间:2014-08-01 11:55:22

标签: php xml

在编写xml文件时,我无法在每个节点后添加行制动。 Xml文件正在以我想要的正确方式写入。但我希望在每个节点完成后添加一个换行符。

以下是我的xml形成代码

<?php

     //Create Database connection
  $mysqli = new mysqli('localhost', 'user', 'pass', 'dbname');

   if(mysqli_connect_errno()) {
      echo "Connection Failed: " . mysqli_connect_errno();
      }
    /* if (!$mysqli->set_charset("utf8")) {
        printf("Error loading character set utf8: %s\n", $mysqli->error);
    } else {
        printf("Current character set: %s\n", $mysqli->character_set_name());
    }
    */
 $xml2 = new SimpleXMLElement('<xml/>');
 $xml2->addAttribute('encoding','UTF-8');
// $xml2->formatOutput = true;    
  //      $xml2->preserveWhiteSpace = false;
    for ($i = 0; $i < 2; $i++) {

    $start = $i * 10000;
    $query = "select tablecolname from tablename limit $start, 10000";
$result = mysqli_query($mysqli,$query);  

//Create SimpleXMLElement object
$xml = new SimpleXMLElement('<node/>');

while($row = mysqli_fetch_array($result)) {
        $mydata = $xml->addChild('internalnode');
$mydata = $xml->addChild('\n');
$mydata->loc=$row['Site'];
//htmlentities(strip_tags($mydata->loc=$row['Siteurl']), ENT_COMPAT,'utf-8');
$mydata = $xml->addChild('\n');
    }
    // used to be: $fp = fopen("folder/file2.xml","wb");
    $fp = fopen("site2/sitemap$i.xml","wb");
    fwrite($fp,utf8_encode($xml->asXML()));
    fclose($fp);
}


    $xml = new SimpleXMLElement('<urlset/>');

    ?>

我当前写的xml文件如下:

<?xml version="1.0">
<node><internalnode>text</internalnode><internalnode>text</internalnode>......</node>

我希望格式应该像。

<?xml version="1.0">
<node>
<internalnode>text</internalnode>
<internalnode>text</internalnode>
.
.
.
</node>

请帮助纠正代码,以便我可以在每个片段后添加行。

2 个答案:

答案 0 :(得分:0)

请参阅@george Brighton here.

的答案

首先需要加载xml并获取其节点值,然后使用array_map()删除前导空格和尾随空格以及array_filter删除空元素。

请参阅我提供的链接中的答案..希望这很有帮助

答案 1 :(得分:0)

这是我为那些正在努力理解SimpleXMLElement如何工作的人做出的贡献。

经过一段时间试图弄清楚它是如何工作的,我已经找到了这个小例子:

<?php
    $xmlstr = "<?xml version='1.0' ?>\n".
              // optionally you can specify a xml-stylesheet for presenting the results. just uncoment the following line and change the stylesheet name.
              /* "<?xml-stylesheet type='text/xsl' href='xml_style.xsl' ?>\n". */
              "<book></book>";

    // create the SimpleXMLElement object with an empty <book> element
    $xml = new SimpleXMLElement($xmlstr);

    // add some child nodes
    $xml->addChild("title", "Title of my book");
    $xml->addChild("abstract", "My book is about learning to work with SimpleXMLElement");

    // add some more child nodes
    $chapter1 = $xml->addChild("chapter_1");
    // add an attribute to child chapter_1
    $chapter1->addAttribute("chapter_title", "Introduction to my book");

    $chapter2 = $xml->addChild("chapter_2");
    $chapter2->addAttribute("chapter_title", "Development of my book");

    $chapter3 = $xml->addChild("chapter_3");
    $chapter3->addAttribute("chapter_title", "Another chapter of my book");

    $conclusion = $xml->addChild("conclusion", "The ending of my book");

    // insert the header to tell the browser how to read the document
    header("Content-type: text/xml");
    // print the SimpleXMLElement as a XML well-formed string
    echo $xml->asXML();
?>

MANUAL