Link to home
Start Free TrialLog in
Avatar of TruthHunter
TruthHunter

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of f_o_o_k_y
f_o_o_k_y
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TruthHunter
TruthHunter

ASKER

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.
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

While it wasn't a solution per se, it did show me that I was heading in the wrong direction.
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

Sorry, there needs to be a closing "}" on the class definition.