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
Switch to threaded view of this topic Create a new topic Submit Reply
building a simple xml object from scratch without dom but with formatting [message #171727] Wed, 19 January 2011 01:40 Go to next message
dan is currently offline  dan
Messages: 1
Registered: January 2011
Karma: 0
Junior Member
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
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 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
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
==================
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 Go to previous messageGo to next message
Jeff North is currently offline  Jeff North
Messages: 58
Registered: November 2010
Karma: 0
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.
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: 0
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
--
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Wed Nov 27 06:04:03 GMT 2024

Total time taken to generate the page: 0.03935 seconds