SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple xml problem

    I use simplexml to write xml to files.
    But the elements are written all to one line.
    How can I format xml to write elements line by line as
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <weather>
    <city>
    test
    </city>
    <city>
    test2
    </city>
    </weather>

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,228
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    AFAIK, not being able to format the output source is a limitation of simpleXML. I guess you might be able to add "\r" and/or "\n" and "\t" to your code to create newlines and indentation (maybe), but if you use one of the other XML classes instead, they are able to do the "less simple" stuff that simpleXML can not.

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can change it to a DomDocument:
    PHP Code:
    $dom dom_import_simplexml($xml);
    $document $dom->ownerDocument;
    $document->xmlStandalone true;
    $document->formatOutput true;
    $document->encoding "utf-8";
    echo 
    $document->saveXML($dom); 

  4. #4
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    You can change it to a DomDocument:
    PHP Code:
    $dom dom_import_simplexml($xml);
    $document $dom->ownerDocument;
    $document->xmlStandalone true;
    $document->formatOutput true;
    $document->encoding "utf-8";
    echo 
    $document->saveXML($dom); 
    I found that also
    Code:
    $doc = new DOMDocument('1.0');
    $doc->preserveWhiteSpace =false;
    $doc->formatOutput = true;
    $doc->loadXML( $xmlobj->asXML());
    echo $doc->saveXML();
    $doc->save($file);

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't tested it, but I would guess that dom_import_simplexml is a lot faster, since it probably doesn't have to marshal and parse in the process.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •