Skip to content

Commit 1e539d6

Browse files
committed
1 parent dd65fa2 commit 1e539d6

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

test.sh

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ function prettyPrint($title, $thing)
1313
$xmlstr = <<<EOD
1414
<tv>
1515
<show name="Family Guy">
16-
<dog type="beagle">Brian</dog>
16+
<dog>Brian</dog>
1717
<kid>Chris</kid>
1818
<kid>Meg</kid>
1919
<kid><![CDATA[<em>Stewie</em>]]></kid>
2020
</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>
2128
<show name="Edge Cases" zero="0" empty="">
2229
<empty></empty>
2330
<foo empty=""></foo>
@@ -30,14 +37,7 @@ $expected = array(
3037
"show" => array(
3138

3239
array(
33-
"dog" => array(
34-
array(
35-
"dog"=>"Brian",
36-
"@attributes" => array(
37-
"type"=>"beagle"
38-
)
39-
)
40-
),
40+
"dog" => 'Brian',
4141
"kid" => array(
4242
"Chris",
4343
"Meg",
@@ -46,6 +46,21 @@ $expected = array(
4646
"@attributes" => array( "name" => "Family Guy" )
4747
),
4848

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+
4964
array(
5065
"empty" => array(),
5166
"foo" => array(

xmlstr_to_array.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ function xmlstr_to_array($xmlstr) {
1616
}
1717

1818
function domnode_to_array($node) {
19-
$alt = false; //Flag for regular output
2019
$output = array();
2120
switch ($node->nodeType) {
2221

@@ -41,22 +40,15 @@ function domnode_to_array($node) {
4140
}
4241
}
4342
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.
4844
}
4945
if(is_array($output)) {
5046
if($node->attributes->length) {
5147
$a = array();
5248
foreach($node->attributes as $attrName => $attrNode) {
5349
$a[$attrName] = (string) $attrNode->value;
5450
}
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;
6052
}
6153
foreach ($output as $t => $v) {
6254
if(is_array($v) && count($v)==1 && $t!='@attributes') {

0 commit comments

Comments
 (0)