FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » building a simple xml object from scratch without dom but with formatting
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: building a simple xml object from scratch without dom but with formatting [message #171730 is a reply to message #171727] Wed, 19 January 2011 08:39 Go to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma:
Senior Member
El 19/01/2011 2:40, dan escribió/wrote:
> I have a tree that i want to generate a printed (string) xml
> representation for, and
> i would like to determine if it is possible to do it purely with
> simple_xml
> (SimpleXMLElement), without any reference to dom (DOMDocument).
> Further, i would like the tree to print out nicely (indented according
> to depth of the leaves in the tree), using only the methods of
> SimpleXMLElement.
>
> So, for example, if the tree is
> <x>
> <y/>
> </x>
> i would like to do something along the lines of:
> $x=new SimpleXMLElement('<x/>');
> $x->addChild('y');
> $rep=$x->asXML();
> echo "$rep\n";
>
> This doesn't work, because i have not added any formatting
> information or blank "text nodes" to the simple xml. So i just get
> <x><y/></x>
> (all on one line) when i print it.
>
> I realize that i could write some code to recursively loop over
> the children of the SimpleXMLElement and get the indentation that
> way, but i would like to stick to the methods that the
> SimpleXMLElement
> directly provides.
>
> It seems that this should be possible in principle, because you can
> read a formatted tree via code like
> $z=simplexml_load_file($file_name);
> and when you print it out via code like
> $w=$z->asXML();
> you get every single space out that you put in.
>
> But how can this be done synthetically (i.e., without
> simplexml_load_file()),
> and without using anything from dom?

Nice question. I don't if such feature exists in SimpleXML but the
asXML() method appears to use the blank space from the original
contents. See for instance:

<?php

$x=new SimpleXMLElement("<x>\n\t</x>");
$x->addChild('y', "\n\t");
$rep=$x->asXML();
echo "$rep\n";

?>

.... which prints:

<?xml version="1.0"?>
<x>
<y>
</y></x>

That's probably why simplexml_load_file() provides nice formatting.



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: newbie: Zend or PEAR supported on web hosting company?
Next Topic: Re: hackbar i ajax
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 15:18:23 GMT 2024

Total time taken to generate the page: 0.05667 seconds