Split XML Element Variable [duplicate] - php

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
I am integrating with an API which returns data in XML format. I receive a response from the API and store it in a variable named $result. How do I access the clientdetails values from the example response below?
<client_get>
<header>
<messagetype>Response</messagetype>
<submissionnumber>563jd94a2jfoi4f</submissionnumber>
</header>
<clientdetails>
<clientid>23373920</clientid>
<companyname>Testing Test</companyname>
<accountreference>1133</accountreference>
<status>LIVE</status>
</clientdetails>
<gocardlessdetails>
<newsignupurl>https://www.url here>
</gocardlessdetails>
</client_get>

If you can call an external process, there are command line splitters out there. One, xmlsplit, has an option to automatically include that header element in each of the split files, but it is commercial. There are also OS XML splitters.

Related

php xml. remove only comment <!-- keep tags and values [duplicate]

This question already has answers here:
Commenting and Un-Commenting a Node in an XML Document
(1 answer)
How to retrieve comments from within an XML Document in PHP
(4 answers)
Closed 1 year ago.
have a xml file that i read with php. In there is a commented section that i wanna read like it wasnt a comment, like it was real in the xml structure. For example:
$xml_str = "<test><tag></tag></test> <!-- <report><tag1>test</tag1><tag2>test2</tag2></report> -->";
How can i do this, that i remove the tag from report? With
foreach ($xpath->query('//comment()') as $comment)
{
var_dump($comment->textContent);
}
i have only the text strings but not the tags and so on. Uncommenting it when reading the file would be great

How to convert large json file to php array? [duplicate]

This question already has answers here:
Processing large JSON files in PHP
(7 answers)
Closed 8 years ago.
Some API is providing json file of 88MB. I need to store in some of its value in database.
$cURL="url here"
$str = file_get_contents($cURL);
$str = json_decode($str);
echo '<pre>';
print_r($str);
this code is not working because file size is too large. I need to convert that json file to a php array. I am not able to parse this file. Any help from anyone?
Quite complicated if the problem should be solved during the query. Caching the preliminary data on the filesystem then separate the values into chunks would be able to solve the issue.

Read xml Node Values In Php [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 8 years ago.
<root>
<status>Call Triggered Successfully</status>
<is_ndnc>no</is_ndnc>
<callid>aaac5814-400f-45ea-9235-f0198e5edd4b</callid>
</root>
how can Read nodes i hv to assign to variable
like
$status='Call Triggered Successfully';
$is_ndnc='no';
$callid='aaac5814-400f-45ea-9235-f0198e5edd4b';
There are many ways in which you can gt the nodevalue from xml in php.
You could use xpath which returns an array of SimpleXMLElement objects.
Or you could load the file with simplexml_load_file() and use the foreach loop as explained here

How to get XML nodes content when names include special Characters? [duplicate]

This question already has answers here:
Simple XML - Dealing With Colons In Nodes
(4 answers)
Closed 9 years ago.
Im trying to navigate an XML block similar to this one ($doc) using PHP simplexml_load_string and using xpath on $doc to get only the 'Day' block like this:
$myday = $doc->xpath ('//Day');
that lets me access all data from the block as an object, meaning
$myday->AdultCount;
returns 1 and
$myday->Id;
returns "6a0"
however I can't access "SpecialDeals" content not using:
$myday->SpecialDeals
nor using:
$myday->SpecialDeals->a:string
Whats is the right syntax in this case?
<Days>
<DaysId>687</DaysId>
<Day>
<AdultsCount>1</AdultsCount>
<Availability>Available</Availability>
<Id>6a0</Id>
<RoomType>Studio</RoomType>
<SpecialDeals xmlns:a="http://microsoft.com/2003/Arrays">
<a:string>Best Day Ever</a:string>
</SpecialDeals>
</Day>
<DaysPrice>247.4</DaysPrice>
</Days>");
You can access the tags with colons in them (aka namespaces) using the children() method:
echo $xml->Day->SpecialDeals->children('a', true)->string[0];
Demo!
This SitePoint article explains namespaces in detail.

passing a php array to a jquery function [duplicate]

This question already has answers here:
Passing PHP variable into JavaScript [duplicate]
(5 answers)
passing PHP objects to javascript [duplicate]
(4 answers)
Closed 9 years ago.
I need to pass a php variable to a jquery function. the php variable is an array of unknown size. is there a way to pass each member of the array to be processed individually by the function?
I Guess you should use JSON to pass the php variable to jquery. its the easiest way to do that.
You can use JSON to print the array and parse that to receive a JS object.
I assume you use AJAX (directly printing into the javascript source is possible but ill-advised).
Your php script needs to print output directly:
print_r(json_encode($data_array));
And on the receiving end use $.parseJSON:
var data_array = $.parseJSON(ajax_result);

Categories