SimpleXML: convert xml to text

Hello, I want to convert some xml stuff to simple string using php, say:

<?php

$string = <<<XML
<a> left <foo>center1<ok>ok</ok>center2</foo>right </a>
XML;

$xml = simplexml_load_string($string);
echo $xml;
?>
The code will output "left right" while all the central stuff was lost.
How should I print it as "left center1 ok center2 right" ?

Thanks in advance, and please Cc me. :)

--
Dasn


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Dasn [ So, 14 März 2010 14:58 ] [ ID #2035025 ]

Re: SimpleXML: convert xml to text

--=-3+ut00E5ie3khAYMRnpK
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Sun, 2010-03-14 at 21:58 +0800, Dasn wrote:

> Hello, I want to convert some xml stuff to simple string using php, say:
>
> <?php
>
> $string = <<<XML
> <a> left <foo>center1<ok>ok</ok>center2</foo>right </a>
> XML;
>
> $xml = simplexml_load_string($string);
> echo $xml;
> ?>
> The code will output "left right" while all the central stuff was lost.
> How should I print it as "left center1 ok center2 right" ?
>
> Thanks in advance, and please Cc me. :)
>
> --
> Dasn
>
>


Can't you just call strip_tags() on the string? As you don't need to
echo any attribute values, I would have thought it would be perfect. As
XML is meant to be well-formed anyway, strip_tags shouldn't cause any
problems with broken or missing closing tags.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-3+ut00E5ie3khAYMRnpK--
Ashley Sheridan [ So, 14 März 2010 15:02 ] [ ID #2035026 ]

Re: SimpleXML: convert xml to text

On Sun, 14 Mar 2010 22:02:22 +0800, Ashley Sheridan wrote:

> On Sun, 2010-03-14 at 21:58 +0800, Dasn wrote:
>
>> Hello, I want to convert some xml stuff to simple string using php, say:
>>
>> <?php
>>
>> $string = <<<XML
>> <a> left <foo>center1<ok>ok</ok>center2</foo>right </a>
>> XML;
>>
>> $xml = simplexml_load_string($string);
>> echo $xml;
>> ?>
>> The code will output "left right" while all the central stuff was lost.
>> How should I print it as "left center1 ok center2 right" ?
>>
>> Thanks in advance, and please Cc me. :)
>>
>
>
> Can't you just call strip_tags() on the string? As you don't need to
> echo any attribute values, I would have thought it would be perfect. As
> XML is meant to be well-formed anyway, strip_tags shouldn't cause any
> problems with broken or missing closing tags.
>
Thanks Ashley for your reply.
Sorry that my description was not clear enough.
Actually I don't want to just strip the tags, sometimes I also wanna
change some elements. For another example:
"<a>bla<function>strip_tags</function>bla</a>". When output, I also want
to make some change to the data depending on the tags, say, trying to
append a pair of "()" to the "<function>" element, printing it as "bla
strip_tags() bla".
The problem is when I processing the nodes with SimpleXML, I couldn't get
the right order of the data. That is :
"echo $xml->a, (string) $xml->a->function . '()'" // prints "bla bla
strip_tags()"

Thank you anyway. Cc please.

--
Dasn


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Dasn [ So, 14 März 2010 23:25 ] [ ID #2035040 ]

Re: SimpleXML: convert xml to text

On Sun, 14 Mar 2010 22:02:22 +0800, Ashley Sheridan wrote:

> On Sun, 2010-03-14 at 21:58 +0800, Dasn wrote:
>
>> Hello, I want to convert some xml stuff to simple string using php, say:
>>
>> <?php
>>
>> $string = <<<XML
>> <a> left <foo>center1<ok>ok</ok>center2</foo>right </a>
>> XML;
>>
>> $xml = simplexml_load_string($string);
>> echo $xml;
>> ?>
>> The code will output "left right" while all the central stuff was lost.
>> How should I print it as "left center1 ok center2 right" ?
>>
>> Thanks in advance, and please Cc me. :)
>>

Well I change to use xmlparser instead, never mind. :)

--
Dasn


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Dasn [ Mo, 15 März 2010 02:07 ] [ ID #2035128 ]
PHP » gmane.comp.php.general » SimpleXML: convert xml to text

Vorheriges Thema: Doing dynamic routing for an office
Nächstes Thema: Change displayed file name to download