Skip to content
This repository
Browse code

Fixing Xml parsing where multiple child elements of the same name, wi…

…th no value are collapsed causing data errors when Xml object was converted to an array. Test cases from 'burzum' added.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8209 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
commit 975e4452d2934168a9140b8744325cf265185316 1 parent ff5d9ed
Mark Story authored July 01, 2009
13  cake/libs/xml.php
@@ -685,6 +685,19 @@ function toArray($camelize = true) {
685 685
 					$out[$child->name] = $value;
686 686
 				}
687 687
 				continue;
  688
+			} elseif (count($child->children) === 0 && $child->value == '') {
  689
+				$value = $child->attributes;
  690
+
  691
+				if (isset($out[$child->name]) || isset($multi[$key])) {
  692
+					if (!isset($multi[$key])) {
  693
+						$multi[$key] = array($out[$child->name]);
  694
+						unset($out[$child->name]);
  695
+					}
  696
+					$multi[$key][] = $value;
  697
+				} else {
  698
+					$out[$key] = $value;
  699
+				}
  700
+				continue;
688 701
 			} else {
689 702
 				$value = $child->toArray($camelize);
690 703
 			}
61  cake/tests/cases/libs/xml.test.php
@@ -680,6 +680,67 @@ function testSimpleParsing() {
680 680
 		$this->assertEqual($source, $result);
681 681
 	}
682 682
 /**
  683
+ * test that elements with empty tag values do not collapse and corrupt data structures
  684
+ *
  685
+ * @access public
  686
+ * @return void
  687
+ **/
  688
+	function testElementCollapsing() {
  689
+		$xmlDataThatFails = '<resultpackage>
  690
+		<result qid="46b1c46ed6208"><![CDATA[46b1c46ed3af9]]></result>
  691
+		<result qid="46b1c46ed332a"><![CDATA[]]></result>
  692
+		<result qid="46b1c46ed90e6"><![CDATA[46b1c46ed69d8]]></result>
  693
+		<result qid="46b1c46ed71a7"><![CDATA[46b1c46ed5a38]]></result>
  694
+		<result qid="46b1c46ed8146"><![CDATA[46b1c46ed98b6]]></result>
  695
+		<result qid="46b1c46ed7978"><![CDATA[]]></result>
  696
+		<result qid="46b1c46ed4a98"><![CDATA[]]></result>
  697
+		<result qid="46b1c46ed42c8"><![CDATA[]]></result>
  698
+		<result qid="46b1c46ed5268"><![CDATA[46b1c46ed8917]]></result>
  699
+		</resultpackage>';
  700
+
  701
+		$Xml = new Xml();
  702
+		$Xml->load('<?xml version="1.0" encoding="UTF-8" ?>' . $xmlDataThatFails);
  703
+		$result = $Xml->toArray(false);
  704
+
  705
+		$this->assertTrue(is_array($result));
  706
+		$expected = array(
  707
+			'resultpackage' => array(
  708
+				'result' => array(
  709
+					0 => array(
  710
+						'value' => '46b1c46ed3af9',
  711
+						'qid' => '46b1c46ed6208'),
  712
+					1 => array(
  713
+						'qid' => '46b1c46ed332a'),
  714
+					2 => array(
  715
+						'value' => '46b1c46ed69d8',
  716
+						'qid' => '46b1c46ed90e6'),
  717
+					3 => array(
  718
+						'value' => '46b1c46ed5a38',
  719
+						'qid' => '46b1c46ed71a7'),
  720
+					4 => array(
  721
+						'value' => '46b1c46ed98b6',
  722
+						'qid' => '46b1c46ed8146'),
  723
+					5 => array(
  724
+						'qid' => '46b1c46ed7978'),
  725
+					6 => array(
  726
+						'qid' => '46b1c46ed4a98'),
  727
+					7 => array(
  728
+						'qid' => '46b1c46ed42c8'),
  729
+					8 => array(
  730
+						'value' => '46b1c46ed8917',
  731
+						'qid' => '46b1c46ed5268'),
  732
+				)
  733
+		));
  734
+		$this->assertEqual(
  735
+			count($result['resultpackage']['result']), count($expected['resultpackage']['result']),
  736
+			'Incorrect array length %s');
  737
+
  738
+		$this->assertFalse(
  739
+			isset($result['resultpackage']['result'][0][0]['qid']), 'Nested array exists, data is corrupt. %s');
  740
+
  741
+		$this->assertEqual($result, $expected);
  742
+	}
  743
+/**
683 744
  * testMixedParsing method
684 745
  *
685 746
  * @access public

0 notes on commit 975e445

Please sign in to comment.
Something went wrong with that request. Please try again.