Friday, 15 April 2011

php - Adding a block of XML as child of a SimpleXMLElement object -


I have this simple assemble object which is similar to the XML setup with the following ...

  $ Xml = & lt; & Lt; & Lt; EoX & lt; Books & gt; & Lt; Book & gt; & Lt; Name & gt; ABCD & lt; / Name & gt; & Lt; / Book & gt; & Lt; / Books & gt; EOX; $ Sx = new simple assemment ($ xml);  

Now I have a name class that includes information. I can also exclude the book's information about the same classes about each book. In the above (nested block) XML format. Example,

  $ book = New Book ('EFGH'); $ Book & gt; GenXML (); ... will generate & lt; Book & gt; & Lt; Name & gt; EFGH & lt; / Name & gt; & Lt; / Book & gt;  

Now I am trying to figure out a way by which I can use this generated XML block and attach it as a child of that type so that it is now Looks ... for example

  // non-existent member method for illustration purposes only $ Sx-> Adxmlbid ($ book-> gnxml ()); ... XML tree now looks like this: & lt; Books & gt; & Lt; Book & gt; & Lt; Name & gt; ABCD & lt; / Name & gt; & Lt; / Book & gt; & Lt; Book & gt; & Lt; Name & gt; EFGH & lt; / Name & gt; & Lt; / Book & gt; & Lt; / Books & gt;  

I have read on SimpleXMlelement, from the document, addChild () will not be done for you because it does not support XML data as a tag value is.

Two solutions First, you can do it with the help of libxml / DOMDocument / SimpleXML: Importing the code is $ sx , create a DOMDocumentFragment and use:

$ doc = dom_import_simplexml ($ sx) - & gt; Master document; $ Piece = $ doc- & gt; CreateDocumentFragment (); $ Fragment- & gt; AppendXML ($ book & gt; genXML ()); $ Doctor & gt; DocumentElement-> AppendChild ($ piece); // Your original $ sx is now already modified

You can also extend from SimpleXMlelement and add a method that is providing it. Using this specific object, you can easily allow the following to be created:

  $ sx = new MySimpleXMLElement ($ xml); $ Sx-> AddXML ($ book & gt; genXML ());  

Another solution is to use an XML library which has already created this feature such as SimpleDOM . You grab and you use insertXML (), which acts as an ADXMLBID () method that you were describing.

  Include 'SimpleDOM.php'; $ Books = simplest_load_string ('Books & gt; & lt; Book & gt; & lt; Names & gt; ABCD & lt; / name & gt; & lt; / book & gt; & lt; / books & Gt; '); $ Books- & gt; InsertXML (' Book & gt; & lt; name & gt; EFGH & lt; / name & gt; & lt; / book & gt;');  

No comments:

Post a Comment