This is my xml data
<?xml version="1.0" encoding="iso-8859-1"?>
<smslist>
<sms>
<cid>FIRSTCID</cid>
<mid>FIRSTMID</mid>
<mb>98389923</mb>
</sms>
<sms>
<cid>SECONDCID</cid>
<mid>SECONDMID</mid>
<mb>76445645</mb>
</sms>
...
</smslist>
How to push cid and mid data to php array like $array = array(("FIRSTCID","FIRSTMID"),("SECONDCID","SECONDMID")...)
Excuse if this is some duplicate question. :)
you can try this:
$xml = new SimpleXMLElement($your_xml_string);
$xml_array=[];
foreach ($xml->smslist->sms as $sms) {
$xml_array[]=array($sms->cid,$sms->mid);
}
By using xml_parse_into_struct() you can convert XML to array, for detailed document check below link.
http://php.net/manual/en/function.xml-parse-into-struct.php
Related
This is XML file.
<?xml version="1.0" encoding="utf-8"?>
<UW xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
<UWdata>
<List>
<IdProduct>1</IdProduct>
<ProductName>product</ProductName>
<ProductNameDE>product</ProductNameDE>
<ProductNameEN>product</ProductNameEN>
<Uf>1</Uf>
<PSIg>1</PSIg>
<Ug>1</Ug>
</List>
</UWdata>
</UW>
$lines_array=file($url);
$lines_string=implode('',$lines_array);
$xml=simplexml_load_string($lines_string) or die("Error: Cannot create object");
I try with this
echo $xml->UWdata[1]->ProductName;
But it doesn't return anything.I want to return Product name.
Sample code, Use simplexml_load_string
<?php
$a = '<?xml version="1.0" encoding="utf-8"?>
<UW xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
<UWdata>
<List>
<IdProduct>1</IdProduct>
<ProductName>product</ProductName>
<ProductNameDE>product</ProductNameDE>
<ProductNameEN>product</ProductNameEN>
<Uf>1</Uf>
<PSIg>1</PSIg>
<Ug>1</Ug>
</List>
</UWdata>
</UW>';
$xml=simplexml_load_string($a) or die("Error: Cannot create object");
echo ($xml->UWdata->List->ProductName);
?>
When you load the xml file using the php simplexml_load_file function to a variable. The veritable becomes an object.
<?php
$xml=simplexml_load_file("/path/to/the/file.xml");
?>
So, in your case, the $xml variable becomes a multi-level object where every elements of xml file are key of the object. Like: UWdata.
So, as $xml is a multi-level object, to access the element under UWdata, under List under ProductName, you have to code like bellow.
echo $xml->UWdata->List->ProductName."<br>";
Here,
UWdata is the key of $xml object.
List is the key of UWdata.
ProductName is the key of List.
Finally, you will get the value of key element ProductName = product
I modified your script and put the xml in an external file called testxml.xml, as it should be. Always separate the function and the data it's supposed to handle. I used your xml like this:
<?xml version="1.0" encoding="utf-8"?>
<UW xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
<UWdata>
<List>
<IdProduct>1</IdProduct>
<ProductName>productTEST</ProductName>
<ProductNameDE>product</ProductNameDE>
<ProductNameEN>product</ProductNameEN>
<Uf>1</Uf>
<PSIg>1</PSIg>
<Ug>1</Ug>
</List>
</UWdata>
</UW>
And with the following script it returns productTEST only.
$xmlstr = file_get_contents('./testxml.xml');
$xml = simplexml_load_string($xmlstr);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
echo $array['UWdata']['List']['ProductName'];
Hope this helps.
//edit:
While I do not know your project, you might want to take a foreach-approach if it is possible for your xml to contain more than one List element
I am having an xml which contains nodes in the sequence below.
<text_value0>text0</text_value0>
<text_value1>text1</text_value1>
<text_value2>text2</text_value2>.............
Can you please suggest How can i read(parse) the value from these type of nodes without reading each and every tag,probably by using any loop or something to get all the values by code.
Try this:
<?php
$myXMLData ="<?xml version='1.0' encoding='UTF-8'?>
<note>
<text_value0>text0</text_value0>
<text_value1>text1</text_value1>
<text_value2>text2</text_value2>
<text_value3>text3</text_value3>
</note>";
$xml = simplexml_load_string($myXMLData);
foreach ($xml as $text){
echo $text."<br>";
}
?>
I get xml object using curl.
But I can not seem to enter xml elements data.
I tried using:
$sxe = new SimpleXMLElement($result);
$final_xml = $sxe->asXML();
echo $final_xml -> Answer -> Status;
With that example I got nothing.
OR
$xml = simplexml_load_string($sxe);
$json = json_encode($xml);
$array = json_decode($json);
With that example I got an array of the xml as a long string and each position in the array was different note in the xml string.
This is the xml output I get, that I want to retrieve data from:
how can I retrieve data from this xml or change it to JSON and then retrieve data?
Thanx
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns7:GetStopMonitoringServiceResponse xmlns:ns3="http://www.siri.org.uk/siri" xmlns:ns4="http://www.ifopt.org.uk/acsb" xmlns:ns5="http://www.ifopt.org.uk/ifopt" xmlns:ns6="http://datex2.eu/schema/1_0/1_0" xmlns:ns7="http://new.webservice.namespace">
<Answer>
<ns3:ResponseTimestamp>2014-12-04T11:11:55.585+02:00</ns3:ResponseTimestamp>
<ns3:ProducerRef>ISR Siri Server (141.10)</ns3:ProducerRef>
<ns3:ResponseMessageIdentifier>89061165</ns3:ResponseMessageIdentifier>
<ns3:RequestMessageRef>000234:1351677777:4684</ns3:RequestMessageRef>
<ns3:Status>true</ns3:Status>
<ns3:StopMonitoringDelivery version="IL2.7">
<ns3:ResponseTimestamp>2014-12-04T11:11:55.585+02:00</ns3:ResponseTimestamp>
<ns3:RequestMessageRef>0</ns3:RequestMessageRef>
<ns3:Status>true</ns3:Status>
<ns3:MonitoredStopVisit>
<ns3:RecordedAtTime>2014-12-04T11:11:00.000+02:00</ns3:RecordedAtTime>
<ns3:ItemIdentifier>1448001046</ns3:ItemIdentifier>
<ns3:MonitoringRef>40262</ns3:MonitoringRef>
<ns3:MonitoredVehicleJourney>
<ns3:LineRef>4687</ns3:LineRef>
<ns3:DirectionRef>3</ns3:DirectionRef>
<ns3:PublishedLineName>12</ns3:PublishedLineName>
<ns3:OperatorRef>3</ns3:OperatorRef>
<ns3:DestinationRef>40110</ns3:DestinationRef>
<ns3:OriginAimedDepartureTime>2014-12-04T10:45:00.000+02:00</ns3:OriginAimedDepartureTime>
<ns3:VehicleLocation>
<ns3:Longitude>34.94065475463867</ns3:Longitude>
<ns3:Latitude>32.428466796875</ns3:Latitude>
</ns3:VehicleLocation>
<ns3:MonitoredCall>
<ns3:StopPointRef>40262</ns3:StopPointRef>
<ns3:ExpectedArrivalTime>2014-12-04T11:12:00.000+02:00</ns3:ExpectedArrivalTime>
</ns3:MonitoredCall>
</ns3:MonitoredVehicleJourney>
</ns3:MonitoredStopVisit>
</ns3:StopMonitoringDelivery>
</Answer>
</ns7:GetStopMonitoringServiceResponse>
</S:Body>
</S:Envelope>
Try this test code:
<pre><?php
$entries = simplexml_load_file("test.xml");
$namespaces = $entries->getNamespaces(true);
var_dump($namespaces);
$ns3s = $entries->children($namespaces['S'])
->Body
->children($namespaces['ns7'])
->GetStopMonitoringServiceResponse
->children()
->Answer
->children($namespaces['ns3']);
var_dump($ns3s);
I am sending array of objects in XML Soap response from my java code to php in my project using WSDL.
I want to store values in php for my project.
I tried many ways but couldn't able to find how to parse my xml and read values. I am not export in xml area.
Please anyone help me for read my values from values.
My SOAP Response Body:
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" \xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:body>
<getactualtimerequestresponse xmlns="http://impl.sample.com">
<getactualtimereturn>.
<ns1:projectlist xmlns:ns1="http://response.sample.com">
<item>
<ns2:userid xmlns:ns2="http://request.sample.com">4</ns2:userid>
<ns3:username xmlns:ns3="http://request.sample.com">Manoj Arun</ns3:username>
</item>
<item>
<ns5:userid xmlns:ns5="http://request.sample.com">5</ns5:userid>
<ns6:username xmlns:ns6="http://request.sample.com">Sethu Raman</ns6:username>
</item>
</ns1:projectlist>
<ns10:message xsi:nil="true" xmlns:ns10="http://response.sample.com"></ns10:message>
</getactualtimereturn>
</getactualtimerequestresponse>
</soapenv:body>
</soapenv:envelope>
projectList is my object created in java.
In PHP:
I tried to read like below but i didn't got anything.
foreach($xml->xpath('//ns:projectList') as $header)
{
foreach($header->item as $userIds)
{
echo $userIds->xpath('//ns:userId');
}
}
Thanks in advance...
$client = new SoapClient('http://url.com?wsdl');
$params = array(Java arg => php value);
$result = $client->Function( $parms );
print_r($result);
New to working with JSON. I have the following XML results. I'd like to offer the same results but in JSON format when requested. My data is coming from a mySQL array.
My problem comes in when I try to have multiple nodes of the same name. Take my XML result for instance:
<results>
<result>
<item_id>1</item_id>
</result>
<result>
<item_id>50</item_id>
</result>
<result>
<item_id>50433</item_id>
</result>
<result>
<item_id>3</item_id>
</result>
</results>
If I simply do something like the following in PHP, my data keeps overwriting eachother.
foreach($result as $key => $value) {
$json["results"]["result"]["item_id"] = $value;
}
It gives me only one line of result which is the last item_id of 3.
What am I overlooking?
You're overwriting your value in your loop because you're not putting it into an array.
$json["results"]["result"]["item_id"] = $value;
should be
$json["results"]["result"]["item_id"][] = $value;