[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

11/30/2007 at 07:57AM PST, ID: 22993323
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.4

Merging XML in PHP using SimpleXML and DOM

Asked by TruthHunter in PHP Scripting Language, Extensible Markup Language (XML), Web Development

Tags: php, merge, xml, simplexml

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!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
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);
	}
}
 
Keywords: Merging XML in PHP using SimpleXML a…
 
Loading Advertisement...
 
[+][-]12/01/07 05:44 PM, ID: 20390026

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP Scripting Language, Extensible Markup Language (XML), Web Development
Tags: php, merge, xml, simplexml
Sign Up Now!
Solution Provided By: f_o_o_k_y
Participating Experts: 1
Solution Grade: B
 
 
[+][-]12/04/07 07:40 AM, ID: 20403600

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/04/07 06:40 PM, ID: 20408553

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-91 / EE_QW_2_20070628