xml into object
Iemand enig idee hoe je xml in een php object kunt plaatsen? Zodat je de xml values terug krijgt?
Ik probeer het momenteel op de volgende manier maar dat wilt niet echt werken, het is trouwens voor mijn soapclient webservice script.
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
$DisplayAttachment = $client->dbDocAttachmentList($srv_name, $db_name, $unid, 'Book_Cover');
echo $DisplayAttachment->getName() . "<br />";
foreach($DisplayAttachment->attachmentslist() as $attachment)
{
echo $child->getName() . ": " . $attachment . "<br />";
}
echo $DisplayAttachment->getName() . "<br />";
foreach($DisplayAttachment->attachmentslist() as $attachment)
{
echo $child->getName() . ": " . $attachment . "<br />";
}
De XML:
Code (php)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:DefaultNamespace">
<soapenv:Header/>
<soapenv:Body>
<urn:SRVNAME>flexdomino/flex2domino</urn:SRVNAME>
<urn:DBNAME>flex/flexdemo.nsf</urn:DBNAME>
<urn:NOTEID>18E6</urn:NOTEID>
<urn:RTFIELD>Book_Cover</urn:RTFIELD>
</soapenv:Body>
</soapenv:Envelope>
<soapenv:Header/>
<soapenv:Body>
<urn:SRVNAME>flexdomino/flex2domino</urn:SRVNAME>
<urn:DBNAME>flex/flexdemo.nsf</urn:DBNAME>
<urn:NOTEID>18E6</urn:NOTEID>
<urn:RTFIELD>Book_Cover</urn:RTFIELD>
</soapenv:Body>
</soapenv:Envelope>
Bedankt!
Gewijzigd op 09/05/2011 10:26:09 door Ben Van de Voorde
Code (php)
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
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
function simpleXMLToArray($xml,
$flattenValues=true,
$flattenAttributes = true,
$flattenChildren=true,
$valueKey='@value',
$attributesKey='@attributes',
$childrenKey='@children'){
$return = array();
if(!($xml instanceof SimpleXMLElement)){return $return;}
$name = $xml->getName();
$_value = trim((string)$xml);
if(strlen($_value)==0){$_value = null;};
if($_value!==null){
if(!$flattenValues){$return[$valueKey] = $_value;}
else{$return = $_value;}
}
$children = array();
$first = true;
foreach($xml->children() as $elementName => $child){
$value = simpleXMLToArray($child, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey);
if(isset($children[$elementName])){
if($first){
$temp = $children[$elementName];
unset($children[$elementName]);
$children[$elementName][] = $temp;
$first=false;
}
$children[$elementName][] = $value;
}
else{
$children[$elementName] = $value;
}
}
if(count($children)>0){
if(!$flattenChildren){$return[$childrenKey] = $children;}
else{$return = array_merge($return,$children);}
}
$attributes = array();
foreach($xml->attributes() as $name=>$value){
$attributes[$name] = trim($value);
}
if(count($attributes)>0){
if(!$flattenAttributes){$return[$attributesKey] = $attributes;}
else{$return = array_merge($return, $attributes);}
}
return $return;
}
$flattenValues=true,
$flattenAttributes = true,
$flattenChildren=true,
$valueKey='@value',
$attributesKey='@attributes',
$childrenKey='@children'){
$return = array();
if(!($xml instanceof SimpleXMLElement)){return $return;}
$name = $xml->getName();
$_value = trim((string)$xml);
if(strlen($_value)==0){$_value = null;};
if($_value!==null){
if(!$flattenValues){$return[$valueKey] = $_value;}
else{$return = $_value;}
}
$children = array();
$first = true;
foreach($xml->children() as $elementName => $child){
$value = simpleXMLToArray($child, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey);
if(isset($children[$elementName])){
if($first){
$temp = $children[$elementName];
unset($children[$elementName]);
$children[$elementName][] = $temp;
$first=false;
}
$children[$elementName][] = $value;
}
else{
$children[$elementName] = $value;
}
}
if(count($children)>0){
if(!$flattenChildren){$return[$childrenKey] = $children;}
else{$return = array_merge($return,$children);}
}
$attributes = array();
foreach($xml->attributes() as $name=>$value){
$attributes[$name] = trim($value);
}
if(count($attributes)>0){
if(!$flattenAttributes){$return[$attributesKey] = $attributes;}
else{$return = array_merge($return, $attributes);}
}
return $return;
}
Als je het namelijk met simplexml doet kun je hem uitlezen door bijv $obj->Body te doen. Je moet wel opletten met de namespaces.
Toevoeging op 09/05/2011 11:44:59:
Zoiets als het volgende werkt niet.
Wat krijg je letterlijk als je var_dump($DisplayAttachment); doet?
Code (php)
1
object(SimpleXMLElement)#4 (1) { ["file"]=> array(2) { [0]=> object(SimpleXMLElement)#5 (3) { ["name"]=> string(8) "ATT1UBD5" ["source"]=> string(8) "ATT1UBD5" ["size"]=> string(5) "29462" } [1]=> object(SimpleXMLElement)#6 (3) { ["name"]=> string(51) "149120_469476713644_717498644_5950953_2388345_n.jpg" ["source"]=> string(51) "149120_469476713644_717498644_5950953_2388345_n.jpg" ["size"]=> string(5) "29462" } } }
Gewijzigd op 09/05/2011 12:11:07 door Ben Van de Voorde
Code (php)
Dat zal waarschijnlijk al werken ;)
Code (php)
Dan.. ? Ik weet niet goed in welke context die is natuurlijk.
Probeer anders eens dit om de structuur van de XML te bekijken:
Gewijzigd op 09/05/2011 14:30:22 door kees Schepers
Hij is in de goede context.
Ik krijg de volgende output als ik het volgende gebruik
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
object(SimpleXMLElement)#4 (1)
{
["file"]=> array(2)
{
[0]=> object(SimpleXMLElement)#5 (3)
{
["name"]=> string(8) "ATT1UBD5"
["source"]=> string(8) "ATT1UBD5"
["size"]=> string(5) "29462"
}
[1]=> object(SimpleXMLElement)#6 (3)
{
["name"]=> string(51) "149120_469476713644_717498644_5950953_2388345_n.jpg"
["source"]=> string(51) "149120_469476713644_717498644_5950953_2388345_n.jpg"
["size"]=> string(5) "29462"
}
}
}
{
["file"]=> array(2)
{
[0]=> object(SimpleXMLElement)#5 (3)
{
["name"]=> string(8) "ATT1UBD5"
["source"]=> string(8) "ATT1UBD5"
["size"]=> string(5) "29462"
}
[1]=> object(SimpleXMLElement)#6 (3)
{
["name"]=> string(51) "149120_469476713644_717498644_5950953_2388345_n.jpg"
["source"]=> string(51) "149120_469476713644_717498644_5950953_2388345_n.jpg"
["size"]=> string(5) "29462"
}
}
}
Gewijzigd op 09/05/2011 14:00:06 door Ben Van de Voorde
Code (php)
1
2
3
4
5
2
3
4
5
$DisplayAttachment = $client->dbDocAttachmentList($srv_name, $db_name, $unid, 'Book_Cover');
foreach($DisplayAttachment->file as $file) {
echo (string)$file['name'] . (string)$file['source'] . (string)$file['size'];
}
foreach($DisplayAttachment->file as $file) {
echo (string)$file['name'] . (string)$file['source'] . (string)$file['size'];
}
Gewijzigd op 09/05/2011 14:59:05 door Ben Van de Voorde
Krijg nu geen error meer maar hij display de echo nu niet
Hoe kan je erachter komen waarom? Door eerst var_dump($xml); boven de foreach te doen. Dan zie je wat de data structuur is van de variabele
Code (php)
1
2
3
4
5
6
2
3
4
5
6
$xml = simplexml_load_string($DisplayAttachment);
$data = $xml->file[0]->name;
echo $data;
$data = $xml->file[1]->name;
echo $data;
$data = $xml->file[0]->name;
echo $data;
$data = $xml->file[1]->name;
echo $data;
maar ik wil het in een foreach hebben
Dit werkt heel erg bedankt!!
Weet je ook waarom?
Nu ga je direct met de -> in de parameters.