Solved

Merging XML in PHP using SimpleXML and DOM

Posted on 2007-11-30
6
10,455 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to count occurrences of each item in an array.

758 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

22 Experts available now in Live!

Get 1:1 Help Now