-1

I am not able to add line braks after each node while writing xml file. Xml files are getting write in proper way as i want. but i want to add a line break after completion of every node.

following is my code for xml formation

<?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/>');

    ?>

My current xml file which is getting written is as follows:

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

i want the format should be like.

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

Please help in correcting code so that i can add line after each fragment.

user3843585
  • 17
  • 1
  • 7
  • Just FYI: Don't ask on Stackoverflow others to get help for correcting your code. Ask a concrete programming question instead that is to the point. Reduce the code example to the *bare* minimum to show your concrete issue. That works best be re-creating it from scratch. You will formulate your question much better and you will get much better results in answers. Also you most often find an existing answer before asking that solves your issue. – hakre Aug 02 '14 at 09:20

2 Answers2

0

See the answer of @george Brighton here.

You first need to load the xml and get its node value and use array_map() remove leading and trailing whitespace and array_filter to remove empty elements.

See the answer in the link i have provided ..Hope that was helpful

Community
  • 1
  • 1
Avinash Babu
  • 6,171
  • 3
  • 21
  • 26
0

here goes my contribution for those whom are struggling to understand how SimpleXMLElement works.

After some time trying to figure out how this works, I've came up to this small example:

<?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

Bishal Paudel
  • 1,896
  • 2
  • 21
  • 28