Solved

Merging XML in PHP using SimpleXML and DOM

Posted on 2007-11-30
6
10,479 Views
Last Modified: 2013-11-19
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

0
Comment
Question by:TruthHunter
  • 5
6 Comments
 
LVL 11

Accepted Solution

by:
f_o_o_k_y earned 500 total points
ID: 20390026
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
 

Author Comment

by:TruthHunter
ID: 20403600
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
 

Author Comment

by:TruthHunter
ID: 20408553
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Closing Comment

by:TruthHunter
ID: 31411933
While it wasn't a solution per se, it did show me that I was heading in the wrong direction.
0
 

Author Comment

by:TruthHunter
ID: 20510953
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
 

Author Comment

by:TruthHunter
ID: 20510957
Sorry, there needs to be a closing "}" on the class definition.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Searchbox Branding 11 40
SQL XML ALL Nodes Compare in function 2 16
app server have enough resources... 2 21
Session timeout 5 13
Read about why website design really matters in today's demanding market.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now