Re: building a simple xml object from scratch without dom but with formatting [message #171728 is a reply to message #171727] |
Wed, 19 January 2011 03:06 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 1/18/2011 8:40 PM, dan wrote:
> Hi All,
>
> 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?
>
> Thanks in advance for any info on this.
>
> dan
Reading is not the same as writing.
What happens if I want a different format for writing the data out, for
instance? Maybe I want everything in one line. Or maybe I want it
indented by 3 spaces instead of 1 tab.
The output from simplexml is as it is - it's simple (which is why it is
so named). If you want it to output in a different format, you need to
format do it yourself.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|