【问题标题】:not able to add line breaks in xml node in php while writing xml file [duplicate]编写xml文件时无法在php中的xml节点中添加换行符[重复]
【发布时间】:2014-08-01 11:55:22
【问题描述】:

在编写 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>

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

【问题讨论】:

  • 仅供参考:不要在 Stackoverflow 上向其他人寻求帮助以更正您的代码。问一个具体的编程问题,而不是重点。将代码示例减少到 bare 最小值以显示您的具体问题。最好从头开始重新创建它。你会更好地表述你的问题,你会得到更好的答案结果。此外,您通常会在提出解决您的问题之前找到一个现有的答案。

标签: php xml


【解决方案1】:

查看@george Brighton here.的答案

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

在我提供的链接中查看答案..希望对您有所帮助

【讨论】:

    【解决方案2】:

    这是我为那些努力理解 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 2012-06-10
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多