Re: building a simple xml object from scratch without dom but with formatting [message #171729 is a reply to message #171728] |
Wed, 19 January 2011 05:11 |
Jeff North
Messages: 58 Registered: November 2010
Karma:
|
Member |
|
|
On Tue, 18 Jan 2011 22:06:00 -0500, in comp.lang.php Jerry Stuckle
<jstucklex(at)attglobal(dot)net>
<ih5kev$a5r$1(at)news(dot)eternal-september(dot)org> wrote:
> | 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";
What are you outputting to? A browser or command prompt?
If a browser the \n and \t are not recognized - you may have to wrap
you echo in
echo "<pre>";
echo $rep;
echo "</pre>";
> | > 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.
|
|
|