Re: Simple question on simplexml

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jochem Maas wrote:

> there is this:
> 
> http://php.net/manual/en/function.simplexml-element-addChild.php
> 
> which will allow adding of string data (so you won't be needing to
> create the new SimpleXMLElement object as per your example below).
> 
> obviously you will have to first load tghe complete xml document
> into simplexml using one of the following:
> 
> http://php.net/manual/en/function.simplexml-load-file.php
> http://php.net/manual/en/function.simplexml-load-string.php

I tried this, with several variations,
and I have come to the conclusion that it is impossible
to add a tree to a node as I asked using only simplexml functions.
If you have such a solution, I would love to see it.

If you would like an example, I might want to add the item:
<book>
  <author>Smith, J</author>
  <title>PHP for dummies</title>
  <publisher>OUP</publisher>
</book>

My solution, for what it is worth, is something like
-------------------------------------
$docA = new DOMDocument;
$docB = new DOMDocument;

$docB->loadXML($book);

$xpath = new DOMXPath($docB);
$nodes = $xpath->query('//catalog/book');
foreach($nodes as $n) {
  $new = $docA->importNode($n, true);
  $docA->documentElement->appendChild($new);
}

$output = $docA->save("/tmp/catalog.xml");
-------------------------------------

> Timothy Murphy wrote:
>> I have a catalog in XML format:
>> <?xml version="1.0" encoding="iso-8859-1" ?>
>> <catalog>
>>   <book>
>>     ...
>>   <book>
>>   <book>
>>     ...
>>   <book>
>>   ...
>> </catalog>
>> 
>> Now I want to add another book,
>> which I have as a SimpleXMLElement:
>> 
>>         $book = new SimpleXMLElement($string);
>> 
>> where $string reads
>>   <book>
>>     ...
>>   <book>
>> 
>> Can I add this new entry to the catalog
>> using SimpleXML functions,
>> or do I have to introduce a DOMDocument?

-- 
Timothy Murphy  
e-mail (<80k only): tim /at/ birdsnest.maths.tcd.ie
tel: +353-86-2336090, +353-1-2842366
s-mail: School of Mathematics, Trinity College, Dublin 2, Ireland

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux