Merging XML in PHP using SimpleXML and DOM

Hi,

I'm having trouble trying to merge two SimpleXML objects (user preference settings) in PHP.  It should be simple enough - the objects have the same root element, and I simply want to merge at the top child level.  However, the code I'm providing here, while it executes, does not work.  At the end of the merge() function, the merged preferences are empty.

I would very much appreciate if someone could tell me what I'm doing wrong or suggest another approach.  Maximum points because I need a solution pretty quickly.  Thanks very much!
Example of desired XML result:
 
Before merge:
 
Old:
<xml version="1.0"/>
<preferences>
<setting1>value1</setting1>
<setting2 attr21="attr21Value" attr22="attr22Value" attr23="attr23Value"/>
</preferences>
 
New:
<xml version="1.0"/>
<preferences>
<setting1>value2</setting1>
<setting3 attr31="attrValue31" attr32="attr32Value" attr33="attrValue33"><setting31>value31</setting31><setting32>value32</setting32></setting3>
</preferences>
 
After merge:
<xml version="1.0"/>
<preferences>
<setting1>value2</setting1>
<setting2 attr21="attr21Value" attr22="attr22Value" attr23="attr23Value"/>
<setting3 attr31="attrValue31" attr32="attr32Value" attr33="attrValue33"><setting31>value31</setting31><setting32>value32</setting32></setting3>
</preferences>
 
-------------------
 
class UserPreferences
{
	const DEFAULT_STRING = "<preferences/>";
 
	$m_preferences = null;
 
	public function __construct()
	{
		$this->m_preferences = new SimpleXMLElement(self::DEFAULT_STRING);
	}
 
	// $new is a SimpleXMLElement containing the preferences to be merged
	public function merge($new)
	{
		$mergedDom = DOMDocument::loadXML(self::DEFAULT_STRING);
 
		$oldDom = DOMDocument::loadXML($this->m_preferences->asXML());
 
		// Compare existing/old preferences with the new
		// preferences.  If a match is found, delete the
		// old preference.
		foreach ($new->children() as $newChild)
		{
			$i = 0;
 
			foreach ($this->m_preferences->children() as $oldChild)
			{
				if ($newChild->asXML() == $oldChild->asXML())
				{
					$oldDom->documentElement->removeChild($oldDom->documentElement->childNodes->item($i));
					$i--;
				}
				else
				{
					$i++;
				}
			}
 
			$newChildDom = DOMDocument::loadXML($newChild->asXML());
			$mergedDom->documentElement->appendChild($mergedDom->importNode($newChildDom, true));
		}
 
		$this->m_preferences = simplexml_import_dom($oldDom);
 
		foreach ($this->m_preferences->children() as $oldChild)
		{
			$oldChildDom = DOMDocument::loadXML($oldChild->asXML());
			$mergedDom->documentElement->appendChild($mergedDom->importNode($oldChildDom, true));
		}
 
		$this->m_preferences = simplexml_import_dom($mergedDom);
	}
}

Open in new window

TruthHunterAsked:
Who is Participating?
 
f_o_o_k_yConnect With a Mentor Commented:
Did you see these warnings??

Warning: DOMDocument::importNode(): Cannot import: Node Type Not Supported in C:\Users\FooKy\MyWork\www\indexddd.php on line 47 Warning: DOMNode::appendChild() expects parameter 1 to be DOMNode, boolean given in C:\Users\FooKy\MyWork\www\indexddd.php on line 47 value31value32
Warning: DOMDocument::importNode(): Cannot import: Node Type Not Supported in C:\Users\FooKy\MyWork\www\indexddd.php on line 47 Warning: DOMNode::appendChild() expects parameter 1 to be DOMNode, boolean given in C:\Users\FooKy\MyWork\www\indexddd.php on line 47 Warning: DOMDocument::importNode(): Cannot import: Node Type Not Supported in C:\Users\FooKy\MyWork\www\indexddd.php on line 55 Warning: DOMNode::appendChild() expects parameter 1 to be DOMNode, boolean given in C:\Users\FooKy\MyWork\www\indexddd.php on line 55 Warning: DOMDocument::importNode(): Cannot import: Node Type Not Supported in C:\Users\FooKy\MyWork\www\indexddd.php on line 55 Warning: DOMNode::appendChild() expects parameter 1 to be DOMNode, boolean given in C:\Users\FooKy\MyWork\www\indexddd.php on line 55

I'll check the rest of code tommorow. But it might be because of these warnings.
Try to get rid of them.
Best Regards
FooKy
0
 
TruthHunterAuthor Commented:
Hi,

Sorry for the delay in replying - my wife just delivered our third child!  So I've been a little distracted.

I have E_ALL turned on but didn't see these warnings.  Maybe importNode() is complaining because the node was a root node (it shoudln't be) and it can't import a root node into an existing structure that already has a root node?

I just discovered the DOMDocument method createDocumentFragment() and will try it - that may be what I need.
0
 
TruthHunterAuthor Commented:
Hi,

Yes, I managed to arrive at a satisfactory solution, listed below, using the createDocumentFragment() method.  If anyone has an improved alternate method however, I'd love to hear it!

Thanks (and points!) to Fooky for replying!
class UserPreferences
{
        const DEFAULT_STRING = "<preferences/>";
 
        $m_preferences = null;
 
        public function __construct()
        {
                $this->m_preferences = new SimpleXMLElement(self::DEFAULT_STRING);
        }
 
        // $new is a SimpleXMLElement containing the preferences to be merged
        public function merge($new)
        {
		$mergedDom = new DOMDocument();
		$mergedDom->loadXML(self::DEFAULT_STRING);
 
		$oldDom = new DOMDocument();
		$oldDom->loadXML($this->m_preferences->asXML());
 
		// Compare existing/old preferences with the new ones.
		// If a new preference matches an old, discard the old
		// (as it is superseded by the new).
		// A match means the old and new match in node name, 				// attribute names and attribute values.
		foreach ($new->children() as $newChildName => $newChildValue)
		{
			$matchName = false;
			$matchAllAttrs = true;
			$i = 0;
 
			foreach ($this->m_preferences->children() as $oldChildName => $oldChildValue)
			{
				if ($newChildName == $oldChildName)
				{
					$matchName = true;
 
					if (count($newChildValue->attributes()) == count($oldChildValue->attributes()))
					{
						foreach ($newChildValue->attributes() as $newChildAttrName => $newChildAttrValue)
						{
							$matchAttrs = false;
 
							foreach ($oldChildValue->attributes() as $oldChildAttrName => $oldChildAttrValue)
							{
								if (($newChildAttrName == $oldChildAttrName) &&
									((string)$newChildAttrValue == (string)$oldChildAttrValue))
								{
									$matchAttrs = true;
									break;
								}
							}
 
							if (!$matchAttrs)
							{
								$matchAllAttrs = false;
								break;
							}
						}
					}
					else
					{
						$matchAllAttrs = false;
					}
				}
 
				if ($matchName && $matchAllAttrs)
				{
					// Discard the old preference.
					$oldDom->documentElement->removeChild($oldDom->documentElement->childNodes->item($i));
					$i--;
				}
 
				$i++;
			}
 
			// Merge the new preference.
			$df = $mergedDom->createDocumentFragment();
			$df->appendXML($newChildValue->asXML());
 
			$mergedDom->documentElement->appendChild($df);
		}
 
		// All new preferences are merged, and any old
		// preferences that matched any new preferences
		// have been discarded, so simply merge the remaining
		// old preferences.
		$old = simplexml_import_dom($oldDom);
 
		foreach ($old->children() as $oldChildValue)
		{
			$df = $mergedDom->createDocumentFragment();
			$df->appendXML($oldChildValue->asXML());
 
			$mergedDom->documentElement->appendChild($df);
		}
 
		// Save the result of the merge.
		$this->m_preferences = simplexml_import_dom($mergedDom);
	}
}

Open in new window

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
TruthHunterAuthor Commented:
While it wasn't a solution per se, it did show me that I was heading in the wrong direction.
0
 
TruthHunterAuthor Commented:
Sorry - the code provided previously was not correct.  The following seems to be.  Hope this helps someone.
class UserPreferences
{
        const DEFAULT_STRING = "<preferences/>";
 
        $m_preferences = null;
 
        public function __construct()
        {
                $this->m_preferences = new SimpleXMLElement(self::DEFAULT_STRING);
        }
 
        // $newPreferences is a SimpleXMLElement containing the preferences to be merged
	protected function _merge($newPreferences)
	{
		$mergedPreferencesDom = new DOMDocument();
		$mergedPreferencesDom->loadXML(self::DEFAULT_STRING);
 
		$oldPreferences = $this->m_preferences;
 
                // Compare existing/old preferences with the new ones.
                // If a new preference matches an old, we'll skip
                // copying the old (as it is superseded by the new).
                // A match means the old and new match in node name,
                // attribute names and attribute values.
                // (Also note that all preferences are assumed to reside
                // directly under the root <preferences> node.)
		foreach ($oldPreferences->children() as $oldPreferenceName => $oldPreference)
		{
			$found = false;
 
			foreach ($newPreferences->children() as $newPreferenceName => $newPreference)
			{
				if ($newPreferenceName == $oldPreferenceName)
				{
					if ((count($newPreference->attributes()) > 0) && (count($oldPreference->attributes()) > 0))
					{
						if (count($newPreference->attributes()) == count($oldPreference->attributes()))
						{
							$foundAllAttrs = true;
 
							foreach ($oldPreference->attributes() as $oldPreferenceAttrName => $oldPreferenceAttr)
							{
								$foundAttr = false;
 
								foreach ($newPreference->attributes() as $newPreferenceAttrName => $newPreferenceAttr)
								{
									if (($newPreferenceAttrName == $oldPreferenceAttrName) &&
										((string)$newPreferenceAttr == (string)$oldPreferenceAttr))
									{
										$foundAttr = true;
										break;
									}
								}
 
								if (!$foundAttr)
								{
									$foundAllAttrs = false;
									break;
								}
							}
 
							$found = $foundAllAttrs;
						}
					}
					else
					{
						$found = true;
					}
				}
 
				if ($found) { break; }
			}
 
			if (!$found)
			{
                                // No match, copy the old preference.
				$df = $mergedPreferencesDom->createDocumentFragment();
				$df->appendXML($oldPreference->asXML());
 
				$mergedPreferencesDom->documentElement->appendChild($df);
			}
		}
 
                // Merge/Copy all new preferences.
		foreach ($newPreferences->children() as $newPreference)
		{
			$df = $mergedPreferencesDom->createDocumentFragment();
			$df->appendXML($newPreference->asXML());
 
			$mergedPreferencesDom->documentElement->appendChild($df);
		}
 
                // Save the merged preferences.
		$this->m_preferences = simplexml_import_dom($mergedPreferencesDom);
	}

Open in new window

0
 
TruthHunterAuthor Commented:
Sorry, there needs to be a closing "}" on the class definition.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.