Page 1 of 1

Needing Assistance Using the DOMDocument Class (Solved)

Posted: Tue Apr 03, 2012 9:51 am
by Bigun
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.

Re: Needing Assistance Using the DOMDocument Class

Posted: Tue Apr 03, 2012 9:58 am
by Celauran
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

Posted: Tue Apr 03, 2012 10:11 am
by Bigun
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?
Keep reading, it covers writing a document too.

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

Posted: Tue Apr 03, 2012 1:39 pm
by tr0gd0rr
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:

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);
	// ...
}
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.

Re: Needing Assistance Using the DOMDocument Class

Posted: Tue Apr 03, 2012 1:43 pm
by tr0gd0rr
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

Posted: Wed Apr 04, 2012 7:09 am
by Bigun
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/
That is quite tempting to use. Thanks for the tip!

Re: Needing Assistance Using the DOMDocument Class

Posted: Thu Apr 05, 2012 8:17 am
by Bigun
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

Posted: Thu Apr 05, 2012 8:48 am
by Bigun
Got it:

Code: Select all

file_put_contents($filename, $xml->saveXML(), LOCK_EX);
Thanks for everything! That class just got my rear out of a sling!

Re: Needing Assistance Using the DOMDocument Class (Solved)

Posted: Thu Apr 05, 2012 2:19 pm
by tr0gd0rr
DOMDocument->save($filename) also saves to a file