Jump to content

problems parsing and dynamically writing to XML using DOM


sunshineinabag

Recommended Posts

As my first post, I'm stuck on 2 problems that I've slowly been managing to figure out. I think (or hope) I'm pretty close to the solution but cant seem to get past this hurdle. Fortunately, both use the same XML file. For the first problem, I created a function that reads from a XML file, stores certain data into an array then returns that array. So far, it's storing the proper information, however, when I do var_dump or print_r, the result looks a little wonky, as though there's a nested array when I'm not creating a nested array  :confused:

function getFileAttrs() {
	$doc = new DOMDocument('1.0');
	$doc->load('newXML.xml');
	$filesAttrs = array();
	$files = $doc->getElementsByTagName("file");
	foreach($files as $file) {
		$fileAttr = $file->getAttribute("filename");
		array_push($filesAttrs, $fileAttr);
	}
	return $filesAttrs;
}

$testarr [] = getFileAttrs();
print_r($testarr);

The above print_r returns the following: 

Array ( [0] => Array ( [0] => file name 9 ) ) 

When I try to loop through the array, I get a notice stating array to string conversion. 

For the second problem, which is more complex, I'm keeping track of file uploads for my website, including who uploaded it and when, as well as who re-uploaded a modified version and when (I assume that if they re-uploaded it, they've modified it in some way but I don't actually examine for any content changes, I may do that later). If the file is being uploaded for the first time, then I create the a brand new file element, otherwise, I find the appropriate file element and simply append by adding new newUser and newDate children. I'm able to get the latter part to work perfectly, however, it seems that it ALWAYS appends to a file element, even when it should be creating one. Basically, I'm using the if-else to find whether the filename exists and it should write to that particular file being examined. This is a bit of a work around since I tried using the parentNode variable to no avail. I am free to use simpleXML, however, I would rather use DOM.  

function appendToXML($filename, $name, $origUser, $origDate, $newUser, $newDate) {
		$doc = new DOMDocument('1.0');
		$doc->load('newXML.xml');
		$doc->formatOutput = true;
		$doc->preserveWhiteSpace = false;
		
		$files = $doc->getElementsByTagName("file");
		foreach($files as $file) {
			$fileAttr = $file->getAttribute("filename");
			if($fileAttr == $filename) {
				$createNewUser = $doc->createElement("newUser");
				$createNewUserText = $doc->createTextNode($newUser);
				$createNewUser->appendChild($createNewUserText);
				$file->appendChild($createNewUser);
				$createNewDate = $doc->createElement("newDate");
				$createNewDateText = $doc->createTextNode($newDate);
				$createNewDate->appendChild($createNewDateText);
				$file->appendChild($createNewDate);
				$doc->documentElement->appendChild($file);
				$doc->save('newXML.xml');
				break;
			} else {
				$createFile = $doc->createElement("file");
				$createFile->setAttribute("filename", $filename);
				$createName = $doc->createElement("name");
				$createNameText = $doc->createTextNode($name);
				$createName->appendChild($createNameText);
				$file->appendChild($createName);
				$createOrigUser = $doc->createElement("origUser");
				$createOrigUserText = $doc->createTextNode($origUser);
				$createOrigUser->appendChild($createOrigUserText);
				$file->appendChild($createOrigUser);
				$createOrigDate = $doc->createElement("origDate");
				$createOrigDateText = $doc->createTextNode($origDate);
				$createOrigDate->appendChild($createOrigDateText);
				$file->appendChild($createOrigDate);
				$createNewUser = $doc->createElement("newUser");
				$createNewUserText = $doc->createTextNode($newUser);
				$createNewUser->appendChild($createNewUserText);
				$file->appendChild($createNewUser);
				$createNewDate = $doc->createElement("newDate");
				$createNewDateText = $doc->createTextNode($newDate);
				$createNewDate->appendChild($createNewDateText);
				$file->appendChild($createNewDate);
				$doc->documentElement->appendChild($file);
				$doc->save('newXML.xml');
				break;
			}
		}
}

And the XML which is giving me all the grief: 

<?xml version="1.0"?>
<users>
<file filename="file name 9">
  <name>blah</name>
  <origUser>bleh</origUser>
  <origDate>blah</origDate>
  <newUser>bleh</newUser>
  <newDate>blah</newDate>
  <newUser>hello</newUser>
  <newDate>hillo</newDate>
  </file>
 </users>

As you can see, I add additional newUser and newDate elements, however, the name, origUser and origDate elements are only created once and never altered afterward. 

Edited by sunshineinabag
Link to comment
Share on other sites

If you don't mean to assign $filesAttrs array (returned by getFileAttrs()) to another nested array outside your function, then remove the left/right square bracket.  The current assignment is adding a new array to $testarr, which then containing the function result.

 

Unless you call your function with different .xml files, I think you just want:

$testarr = getFileAttrs();
Edited by rwhite35
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.