Needing Assistance Using the DOMDocument Class (Solved)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Needing Assistance Using the DOMDocument Class (Solved)

Post 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.
Last edited by Bigun on Thu Apr 05, 2012 8:49 am, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Needing Assistance Using the DOMDocument Class

Post 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?
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: Needing Assistance Using the DOMDocument Class

Post 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.
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Needing Assistance Using the DOMDocument Class

Post 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.
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Needing Assistance Using the DOMDocument Class

Post 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/
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: Needing Assistance Using the DOMDocument Class

Post 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!
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: Needing Assistance Using the DOMDocument Class

Post 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?
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: Needing Assistance Using the DOMDocument Class

Post 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!
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Needing Assistance Using the DOMDocument Class (Solved)

Post by tr0gd0rr »

DOMDocument->save($filename) also saves to a file
Post Reply