File tree Expand file tree Collapse file tree 2 files changed +26
-19
lines changed Expand file tree Collapse file tree 2 files changed +26
-19
lines changed Original file line number Diff line number Diff line change @@ -13,11 +13,18 @@ function prettyPrint($title, $thing)
13
13
$ xmlstr = <<<EOD
14
14
<tv>
15
15
<show name="Family Guy">
16
- <dog type="beagle" >Brian</dog>
16
+ <dog>Brian</dog>
17
17
<kid>Chris</kid>
18
18
<kid>Meg</kid>
19
19
<kid><![CDATA[<em>Stewie</em>]]></kid>
20
20
</show>
21
+ <show name="American Dad!">
22
+ <pet type="fish">Klaus</pet>
23
+ <alien nick="The Decider">
24
+ <persona>Roger Smith</persona>
25
+ <persona>Sidney Huffman</persona>
26
+ </alien>
27
+ </show>
21
28
<show name="Edge Cases" zero="0" empty="">
22
29
<empty></empty>
23
30
<foo empty=""></foo>
@@ -30,14 +37,7 @@ $expected = array(
30
37
"show " => array (
31
38
32
39
array (
33
- "dog " => array (
34
- array (
35
- "dog " =>"Brian " ,
36
- "@attributes " => array (
37
- "type " =>"beagle "
38
- )
39
- )
40
- ),
40
+ "dog " => 'Brian ' ,
41
41
"kid " => array (
42
42
"Chris " ,
43
43
"Meg " ,
@@ -46,6 +46,21 @@ $expected = array(
46
46
"@attributes " => array ( "name " => "Family Guy " )
47
47
),
48
48
49
+ array (
50
+ 'pet ' => array (
51
+ "@content " => 'Klaus ' ,
52
+ "@attributes " => array ( "type " => "fish " )
53
+ ),
54
+ 'alien ' => array (
55
+ 'persona ' => array (
56
+ 'Roger Smith ' ,
57
+ 'Sidney Huffman '
58
+ ),
59
+ "@attributes " => array ( "nick " => "The Decider " )
60
+ ),
61
+ "@attributes " => array ( "name " => "American Dad! " )
62
+ ),
63
+
49
64
array (
50
65
"empty " => array (),
51
66
"foo " => array (
Original file line number Diff line number Diff line change @@ -16,7 +16,6 @@ function xmlstr_to_array($xmlstr) {
16
16
}
17
17
18
18
function domnode_to_array ($ node ) {
19
- $ alt = false ; //Flag for regular output
20
19
$ output = array ();
21
20
switch ($ node ->nodeType ) {
22
21
@@ -41,22 +40,15 @@ function domnode_to_array($node) {
41
40
}
42
41
}
43
42
if ($ node ->attributes ->length && !is_array ($ output )) { //Has attributes but isn't an array
44
- $ a = array ();
45
- $ nodeName = $ node ->nodeName ;
46
- $ output = array (0 =>array ($ nodeName =>$ output )); //Change output into an array.
47
- $ alt = true ; //Change the output
43
+ $ output = array ('@content ' =>$ output ); //Change output into an array.
48
44
}
49
45
if (is_array ($ output )) {
50
46
if ($ node ->attributes ->length ) {
51
47
$ a = array ();
52
48
foreach ($ node ->attributes as $ attrName => $ attrNode ) {
53
49
$ a [$ attrName ] = (string ) $ attrNode ->value ;
54
50
}
55
- if ($ alt ){
56
- $ output [0 ]['@attributes ' ] = $ a ; //Add as an array for consistency
57
- }else {
58
- $ output ['@attributes ' ] = $ a ;
59
- }
51
+ $ output ['@attributes ' ] = $ a ;
60
52
}
61
53
foreach ($ output as $ t => $ v ) {
62
54
if (is_array ($ v ) && count ($ v )==1 && $ t !='@attributes ' ) {
You can’t perform that action at this time.
0 commit comments