Needing Assistance Using the DOMDocument Class (Solved)
Moderator: General Moderators
Needing Assistance Using the DOMDocument Class (Solved)
I've been tasked with constructing an XML document. I've never done one before, and so I did some googling and found this document detailing how to use the DOMDocument class which should serve creating a simple XML document. However, I am dealing with a much, much more complex document structure. I've been diagramming the DOM and I'm at about the 30% mark as it stands, here is a sampling.
I'm having a hard time grasping the concept of the appendChild and other various methods to construct the document and need some assistance grasping the concepts. Any help is appreciated.
I'm having a hard time grasping the concept of the appendChild and other various methods to construct the document and need some assistance grasping the concepts. Any help is appreciated.
Last edited by Bigun on Thu Apr 05, 2012 8:49 am, edited 1 time in total.
Re: Needing Assistance Using the DOMDocument Class
Are you parsing an XML document or creating one? You say constructing, but link to a post about reading one. Can you provide some sample code and some specific details about where you're encountering problems?
Re: Needing Assistance Using the DOMDocument Class
Keep reading, it covers writing a document too.Celauran wrote:Are you parsing an XML document or creating one? You say constructing, but link to a post about reading one. Can you provide some sample code and some specific details about where you're encountering problems?
Yes, I'm creating a document, the only code I have is the code presented as an example.
BTW, I finished the DOM diagram, I've made it so it is a little easier to read, I'll change the link in the original post.
Re: Needing Assistance Using the DOMDocument Class
If you are familiar with JavaScript `document.createElement()` and `Element#appendChild()` DOMDocument is the same. In your png example, you can create each node using `$doc->createElement()` and then appending them as you specify. For example:
In my opinion, creating a document like that is hard to read and maintain. I would create nested associative arrays that match the format you need then run it through a function to convert an array structure to XML for you. Let me see if I can find such a function.
Code: Select all
// create a document object
$doc = new DOMDocument();
// create our first node and append it to the document
$PublishedContent = $doc->createElement('PublishedContent');
$doc->appendChild($PublishedContent);
// create DocumentList as a child of PublishedContent and append it
$DocumentList = $doc->createElement('DocumentList');
$PublishedContent->appendChild($DocumentList);
// iterate through some database records and create Document elements
foreach ($myDocumentRecords as $record) {
// create Document element and append to DocumentList
$Document = $doc->createElement('Document');
$DocumentList->appendChild($Document);
// create Captured, set a value (i.e. <Captured>%value%</Captured>), and append to Document
$Captured = $doc->createElement('Captured');
$Captured->nodeValue = $record->captured;
$Document->appendChild($Captured);
// ...
}Re: Needing Assistance Using the DOMDocument Class
Here is an array-to-XML converter that is recent and looks simple: http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes/
Re: Needing Assistance Using the DOMDocument Class
That is quite tempting to use. Thanks for the tip!tr0gd0rr wrote:Here is an array-to-XML converter that is recent and looks simple: http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes/
Re: Needing Assistance Using the DOMDocument Class
I've almost got it complete, after wrapping my head around the idea of nested arrays (some 6 deep or so), I'm managed to mimic the entire XML scheme. I need help with one last simple thing. In all the examples on the page, they explain how to echo the XML to the terminal, how do I save it to a file?
Re: Needing Assistance Using the DOMDocument Class
Got it:
Thanks for everything! That class just got my rear out of a sling!
Code: Select all
file_put_contents($filename, $xml->saveXML(), LOCK_EX);Re: Needing Assistance Using the DOMDocument Class (Solved)
DOMDocument->save($filename) also saves to a file