This is my xml code
<Hotelobjekt xmlns="http://www.dcs-caesar.de/hoteldaten" CRSSperre="false" Buchungssperre="false">
<Hotelcode>AYTBLK0038</Hotelcode>
<Hotelname>LIMAK ATLANTIS DE LUXE HOTEL & RESORT</Hotelname>
<Saisondefinition Saisonkuerzel="W16" DatumVon="2016-11-01" DatumBis="2017-04-06">
<Saisonzeit Nummer="1">
<Zeitraum DatumVon="2016-11-01" DatumBis="2016-11-18"/>
<Zeitraum DatumVon="2016-12-25" DatumBis="2016-12-31"/>
</Saisonzeit>
<Saisonzeit Nummer="2">
<Zeitraum DatumVon="2017-01-01" DatumBis="2017-03-06"/>
<Zeitraum DatumVon="2016-11-19" DatumBis="2016-12-24"/>
</Saisonzeit>
<Saisonzeit Nummer="3">
<Zeitraum DatumVon="2017-03-07" DatumBis="2017-04-06"/>
</Saisonzeit>
</Saisondefinition>
</Hotelobjekt>
$items = simplexml_load_file($url);
print_r($items); // it's okey works
array output;
SimpleXMLElement Object ( [#attributes] => Array ( [CRSSperre] =>
false [Buchungssperre] => false ) [Hotelcode] => AYTBLK0038
[Hotelname] => LIMAK ATLANTIS DE LUXE HOTEL & RESORT
[Saisondefinition] => SimpleXMLElement Object ( [#attributes] => Array
( [Saisonkuerzel] => W16 [DatumVon] => 2016-11-01 [DatumBis] =>
2017-04-06 ) [Saisonzeit] => Array ( [0] => SimpleXMLElement Object (
[#attributes] => Array ( [Nummer] => 1 ) [Zeitraum] => Array ( [0] =>
SimpleXMLElement Object ( [#attributes] => Array ( [DatumVon] =>
2016-11-01 [DatumBis] => 2016-11-18 ) ) [1] => SimpleXMLElement Object
( [#attributes] => Array ( [DatumVon] => 2016-12-25 [DatumBis] =>
2016-12-31 ) ) ) ) [1] => SimpleXMLElement Object ( [#attributes] =>
Array ( [Nummer] => 2 ) [Zeitraum] => Array ( [0] => SimpleXMLElement
Object ( [#attributes] => Array ( [DatumVon] => 2017-01-01 [DatumBis]
=> 2017-03-06 ) ) [1] => SimpleXMLElement Object ( [#attributes] => Array ( [DatumVon] => 2016-11-19 [DatumBis] => 2016-12-24 ) ) ) ) [2]
=> SimpleXMLElement Object ( [#attributes] => Array ( [Nummer] => 3 ) [Zeitraum] => SimpleXMLElement Object ( [#attributes] => Array (
[DatumVon] => 2017-03-07 [DatumBis] => 2017-04-06 ) ) ) ) ) )
echo $items->Hotelcode."<br>";
echo $items->Hotelname."<br>";
this code works but I want to get the other code
<Zeitraum DatumVon="2017-01-01" DatumBis="2017-03-06"/>
Just do loop with child items
$xml = simplexml_load_string($string);
foreach($xml->Saisondefinition->Saisonzeit as $Saisonzeit) {
echo $Saisonzeit['Nummer'] . "<br>\n";
foreach($Saisonzeit->Zeitraum as $Zeitraum ) {
echo $Zeitraum['DatumVon'] . ' - ' . $Zeitraum['DatumBis'] . "<br>\n";
}
}
Demo on eval.in
Related
Below is the code snippet for printing values in a PDF report using codeigniter.
foreach ($data as $key => $getData)
{
$html1 = $this->load->view('pdf/pod2', $getData, true);
//echo $html;exit;
$this->load->library('pdf');
$pdf = $this->pdf->load();
$pdf->setAutoTopMargin = 'stretch';
$pdf->setAutoBottomMargin = 'stretch';
//$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822));
$pdf->WriteHTML($html1);
}
If i do echo '<pre>';print_r($data);exit;
I get the following output
Array
(
[316] => Array
(
[CNote] => stdClass Object
(
[Consignment_Details_id] => 316
)
[Invoice_Nos] => Array
(
[0] => stdClass Object
(
[invoice] => 161052
)
[1] => stdClass Object
(
[invoice] => 161053
)
)
[Invoice_Copies] => Array
(
)
[CNote_data] => Array
(
)
[no_packages] => 0
[driver] =>
)
[317] => Array
(
[CNote] => stdClass Object
(
[Consignment_Details_id] => 317
)
[Invoice_Nos] => Array
(
[0] => stdClass Object
(
[invoice] => 161066
)
)
[Invoice_Copies] => Array
(
)
[CNote_data] => Array
(
)
[no_packages] => 0
[driver] =>
)
[318] => Array
(
[CNote] => stdClass Object
(
[Consignment_Details_id] => 318
)
[Invoice_Nos] => Array
(
[0] => stdClass Object
(
[invoice] => 161067
)
)
[Invoice_Copies] => Array
(
)
[CNote_data] => Array
(
)
[no_packages] => 0
[driver] =>
)
)
Now the problem is in the pdf report only the last array values is being displayed
i.e 318
Actual scenario what i need is o display three array values but only last value is getting displayed. How to solve this, any help appreciated.
First array:
[VariationSpecificsSet] => SimpleXMLElement Object
(
[NameValueList] => Array
(
[0] => SimpleXMLElement Object
(
[Name] => Size
[Value] => Array
(
[0] => 5FT King Size
[1] => 4FT6 Double
)
)
[1] => SimpleXMLElement Object
(
[Name] => Main Colour
[Value] => Array
(
[0] => Brown
[1] => Black
)
)
)
)
Second Array:
[Variation] => SimpleXMLElement Object
(
[StartPrice] => 14.99
[Quantity] => 12
[VariationSpecifics] => SimpleXMLElement Object
(
[NameValueList] => SimpleXMLElement Object
(
[Name] => Size
[Value] => No.10-1M
)
)
)
examine above two arrays
i want to store value NameValueList in database but the problem is sometimes it is SimpleXMLElement Object and sometimes it is Array
how can i store them ...??
You can detect is by is_array().
$myVal=$test['NameValueList'];
if(is_array($myVal) && count($myVal)>0){
foreach($myVal as $item){
echo $item->Name.":".echo $item->Value;
}
} else {
echo $myVal->Name.":".echo $myVal->Value;
}
Did you tried using json_encode like below.
You can convert the object to array.
$array=json_decode(json_encode($object),true);
I am pulling data from an external site using simplexml_load_file and need to dump into my local database. I do not know how to parse the data so that I can perform my sql insert. Please tell me how to grab the column headings and values from the simplexml_load_file results. Because I may not always know the heading names, Ideally I would like to pull the heading and value in as variables and load them both into sql as values to two columns; column_nam and column_value. Below is an example of the data usnig the print_r
SimpleXMLElement Object
(
[#attributes] => Array
(
[sessionid] => p_panda143ccfcf692ede95e|43413dc74e459ecb|41b5f89600000000|41c18afc4b000000|
)
[OBJECT] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => clinicalreport
[op] => search_filedelivery
)
[reports_viewed] => SimpleXMLElement Object
(
)
[reports_delivered] => SimpleXMLElement Object
(
)
[clinicalreport] => 3006163008
[orders_transmitted] => SimpleXMLElement Object
(
)
[report_request_date] => 10/7/2014 9:17 AM
[reports_undelivered] => SimpleXMLElement Object
(
)
[person_middle_name] => R
[reports_printed] => SimpleXMLElement Object
(
)
[creation_date] => SimpleXMLElement Object
(
)
[ownerid] => 3003154010
[reports_count] => SimpleXMLElement Object
(
)
[sponsor_name] => LabCorp Birmingham
[receivingorganization] => 3003154010
[organization_name] => SimpleXMLElement Object
(
)
[sponsor] => 1502182
[document] => SimpleXMLElement Object
(
)
[clearance] => 10
[report_subject] => HL7 Order Copy
[report_service_date] => 10/7/2014 9:19 AM
[reports_forwarded] => SimpleXMLElement Object
(
)
[forwarded] => SimpleXMLElement Object
(
)
[std_orders_created] => SimpleXMLElement Object
(
)
[person_last_name] => Angles
[is_viewed] => SimpleXMLElement Object
(
)
[is_latest] => SimpleXMLElement Object
(
)
[is_abnormal] => SimpleXMLElement Object
(
)
[expiration_date] => 4/5/2015 9:19 AM
[sequence] => SimpleXMLElement Object
(
)
[is_annotated] => SimpleXMLElement Object
(
)
[originalreport] => SimpleXMLElement Object
(
)
[person_account_number] => 1799
[filler_order_number] => SimpleXMLElement Object
(
)
[placer_order_number] => SimpleXMLElement Object
(
)
[is_forwarded] => SimpleXMLElement Object
(
)
[report_priority] => SimpleXMLElement Object
(
)
[person] => SimpleXMLElement Object
(
)
[receiving_cg_fname] => SimpleXMLElement Object
(
)
[creation_datetime] => 10/7/2014 9:19 AM
[receiving_cg_lname] => SimpleXMLElement Object
(
)
[is_downloaded] => N
[receiving_cg_mname] => SimpleXMLElement Object
(
)
[report_status] => NA
[lab_code] => MB
[ordering_cg_suffix] => SimpleXMLElement Object
(
)
[ordering_cg_fname] => SimpleXMLElement Object
(
)
[ordering_client_id] => 8001631
[person_first_name] => Madeleine
[report_type] => ORDER
[ordering_cg_lname] => SimpleXMLElement Object
(
)
[ordering_cg_mname] => SimpleXMLElement Object
(
)
[previousreport] => SimpleXMLElement Object
(
)
[orderresult] => SimpleXMLElement Object
(
)
[receiving_cg_suffix] => SimpleXMLElement Object
(
)
[psc_orders_created] => SimpleXMLElement Object
(
)
[receiving_client_id] => 8001631
[is_printed] => SimpleXMLElement Object
(
)
[content_expiration_date] => 2/4/2015 9:19 AM
[person_suffix] => SimpleXMLElement Object
(
)
[abnormals_count] => SimpleXMLElement Object
(
)
)
)
I found the answer. But first why is there a '-1' by my question.
SOLUTION - I needed to send the results of the simplexml_load_file in the new function xml2array. this function provided me with each name and value.
$myXML = simplexml_load_file($url);
function xml2array($myXML) {
$arr = array();
foreach ($myXML as $element) {
$tag = $element->getName();
$e = get_object_vars($element);
if (!empty($e)) {
$arr[$tag] = $element instanceof SimpleXMLElement ? xml2array($element) : $e;
}
else {
$arr[$tag] = trim($element);
}
}
return $arr;
}
I found the function here
http://bookofzeus.com/articles/convert-simplexml-object-into-php-array/
AI have an XML which have attributes as well as values in them. I want to convert it to an Array or Array Object along with attributes and values.
XML
<?xml version="1.0" encoding="UTF-8"?>
<itemBody>
<div label="options">
<optionchoices optionIdentifier="RESPONSE" shuffle="false" maxOptions="1">
<choice identifier="A1"><![CDATA[aaaa]]></choice>
<choice identifier="A2"><![CDATA[bbbb]]></choice>
<choice identifier="A3"><![CDATA[cccc]]></choice>
</optionchoices>
</div>
</itemBody>
I tried two set of code but the result was not as expected.
Code 1
<?php
$xml = simplexml_load_file('test.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
echo "<pre>";print_r($xml);echo "</pre>"; exit;
?>
Output
SimpleXMLElement Object
(
[div] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => options
)
[optionchoices] => SimpleXMLElement Object
(
[#attributes] => Array
(
[optionIdentifier] => RESPONSE
[shuffle] => false
[maxOptions] => 1
)
[choice] => Array
(
[0] => aaaa
[1] => bbbb
[2] => cccc
)
)
)
)
In the above output if we check then in choice node we get the values only and not the attributes
Code 2
<?php
$xml = simplexml_load_file('test.xml');
echo "<pre>";print_r($xml);echo "</pre>"; exit;
?>
Output
SimpleXMLElement Object
(
[div] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => options
)
[optionchoices] => SimpleXMLElement Object
(
[#attributes] => Array
(
[optionIdentifier] => RESPONSE
[shuffle] => false
[maxOptions] => 1
)
[choice] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A1
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A2
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A3
)
)
)
)
)
)
In this output we get only attributes of XML.
Now what I want is to obtain Attributes as well as Values of the XML.
Please help.
Thanks in advance.
This is what I got. And this is the solution which I expected.
http://outlandish.com/blog/xml-to-json/
I am using php to consume a web service (in coldfusion) to validate against the active directory. My code is below.
<?php
$racf = $_SERVER['AUTH_USER'];
//echo $racf;
//echo $myracf = trim($racf, "FEDERATED\.");
//get authenticated user
$arrUser = explode("\\", $_SERVER["LOGON_USER"]);
$racf = $arrUser[1];
echo $racf.'<br ><br >';
$logins = "http://acoldfusionwebservice/login.cfc?method=loginad&racf=$racf";
if( ! $xml = simplexml_load_file($logins) )
{
echo 'unable to load XML file';
}
else
{
//echo 'XML file loaded successfully <br />';
print_r ($xml);
}
?>
And this produces the following.
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 1.0
)
[header] => SimpleXMLElement Object
(
)
[data] => SimpleXMLElement Object
(
[recordset] => SimpleXMLElement Object
(
[#attributes] => Array
(
[rowCount] => 1
[fieldNames] => cn,mail,givenName,sn
)
[field] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => cn
)
[string] => B000000
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => mail
)
[string] => john.doe#company.com
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => givenName
)
[string] => John
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => sn
)
[string] => Doe
)
)
)
)
)
Can someone help me to parse this information so that I can assign variables and use them. Thanks.
Try
$xml->data->recordset->field[0]->string
I think that field[0] is an Object again