can't access values on parsing xml php [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 trying to parse the xml response returned by url. I can successfully view the response but when I try to get the specific value from the xml, then I get nothing. Here is my PHP code.
$responseMessage = file_get_contents($myurl);
$xml = simplexml_load_string($responseMessage, 'SimpleXMLElement', LIBXML_NOCDATA);
print_r($xml)
Here's the actual xml response
<PPResponse Result="000" Key="99fd1c21-b07d-41f5-bbf8-1917f53c3152">
<ResultMessage>
Operation is succesfully completed
</ResultMessage>
<UtilityInfo>
<UtilityCode>78</UtilityCode>
</UtilityInfo>
<BillInfo>
<Bill>
<BillNumber>99fd1c21-b07d-41f5-bbf8-1917f53c3152</BillNumber>
<DueDate>2015-08-06T11:23:49</DueDate>
<Amount>0</Amount>
<ReserveInfo>Some info</ReserveInfo>
<BillParam>
<mask>3</mask>
<commission type="0" val="0.00" op="-" paysource="1" />
</BillParam>
<RefStan>7676422901773</RefStan>
</Bill>
</BillInfo>
</PPResponse>
Now I want to save only BillNumber value and RefStan Value into some variable so that I can use it later.
How can I do So?

You want to get attribute val from commission property? Try something like this:
$value = (string)$xml->BillInfo->Bill->BillParam->commission->attributes()->val;

Try
$str = <<<XML
<PPResponse Result="000" Key="99fd1c21-b07d-41f5-bbf8-1917f53c3152">
<ResultMessage>
Operation is succesfully completed
</ResultMessage>
<UtilityInfo>
<UtilityCode>78</UtilityCode>
</UtilityInfo>
<BillInfo>
<Bill>
<BillNumber>99fd1c21-b07d-41f5-bbf8-1917f53c3152</BillNumber>
<DueDate>2015-08-06T11:23:49</DueDate>
<Amount>0</Amount>
<ReserveInfo>Some info</ReserveInfo>
<BillParam>
<mask>3</mask>
<commission type="0" val="0.00" op="-" paysource="1" />
</BillParam>
<RefStan>7676422901773</RefStan>
</Bill>
</BillInfo>
</PPResponse>
XML;
$xml = new SimpleXMLElement($str);
echo $xml->ResultMessage;
echo $xml->BillInfo->Bill->ReserveInfo;
Note Please replace $str with $responseMessage.

Related

How do I parse a specific child of xml output using simplexml? [duplicate]

This question already has answers here:
SimpleXML: Selecting Elements Which Have A Certain Attribute Value
(2 answers)
Closed 5 years ago.
I wish to parse this XML output, but only fetch the value for the "doi" record (e.g. 10.1038/onc.2012.390)
<ArticleIdList>
<ArticleId IdType="pubmed">22986524</ArticleId>
<ArticleId IdType="pii">onc2012390</ArticleId>
<ArticleId IdType="doi">10.1038/onc.2012.390</ArticleId>
</ArticleIdList>
Can some advise me how to accomplish this?
I've used
$xml = simplexml_load_file($query) or die("Error, feed not loading");
to create the object, but could not figure out the right syntax to move fw..
Thanks!
With SimpleXMLElement::xpath ( string $path ) function:
$xml = simplexml_load_file($query) or die("Error, feed not loading");
$article_doi = (string) $xml->xpath('//ArticleId[#IdType="doi"]')[0];
print_r($article_doi);
The output:
10.1038/onc.2012.390
http://php.net/manual/en/simplexmlelement.xpath.php

In PHP how do you get the simple text of an XML attribute only XML? [duplicate]

This question already has answers here:
Accessing #attribute from SimpleXML
(10 answers)
Closed 7 years ago.
I have an XML that's coming back from an cURL post that's returning:
$output=<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mtMessageRsp carrier="102" messageId="769f2e4f-56da-4865-988a-a9199c387a48"/>
I'm returning this as an XML via:
return simplexml_load_string($output);
$result is catching the return and it's coming back as:
$result = {
"#attributes" : {
carrier : "102",
messageId : "8d691cbe-d188-42b1-9041-387666d39c6a"
}
How can I drill down to get the messageId as plane text? When I use this:
$result['messageId']
I get:
{
"0" : "bf629ae9-c86a-486a-bfb0-704e16448ddf"
}
But I just want:
bf629ae9-c86a-486a-bfb0-704e16448ddf
Figured it out in another post:
$msgID = (string) $result['messageId'];
You have to cast simpleXML Object to a string.
POST: Get value from SimpleXMLElement Object

parse and process HTML/XML/plain text page [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
I am creating a small php app that pulls data from a remote website its working great but i would like to make it more user friendly now.
I need to get a few specific items from the page and as far as I can tell the page looks like an xml file wen you look at sorce code but it has no style to it and appears as plain text so I don't really know what to do.
The page I am trying to get looks like this
<channel>
<name>data</name>
<id>data</id>
<img>data</img>
<auther>data</auther>
<mp3>data</mp3>
<bio>data</bio>
</channel>
<channel>
<name>data</name>
<id>data</id>
<img>data</img>
<auther>data</auther>
<mp3>data</mp3>
<bio>data</bio>
</channel>
<channel>
<name>data</name>
<id>data</id>
<img>data</img>
<auther>data</auther>
<mp3>data</mp3>
<bio>data</bio>
</channel>
<channel>
<name>data</name>
<id>data</id>
<img>data</img>
<auther>data</auther>
<mp3>data</mp3>
<bio>data</bio>
</channel>
I need to get all the data from each tag under the channel tag and keep it in the same order to echo it back out onto my own page in the same way.
How could i do this ? i tried using regex with the following patter
$pattern = '<channel>
<name>(.*)</name>
<id>(.*)</id>
<img>(.*)</img>
<auther>(.*)</auther>
<mp3>(.*)</mp3>
<bio>(.*)</bio>
</channel>';
but that doesn't work I really need the best and simplest way to do this.
$SimpleXMLElement = new SimpleXMLElement($str);
foreach ($SimpleXMLElement->children() as $Channel) {
foreach ($Channel->children() as $Child) {
echo $Child->getName() . ' = ' . (string) $Child;
}
}
this way you can use SimpleXMLElement, it's very easy
I would "sanitize" the incoming data and make an xml document out of it. This can be done by simply wrapping it into a surrounding tag. (I name it channels). Having this, you can parse the data using DOM:
// Sanitize input data. Make an xml out of it
$xml = '<channels>';
$xml .= file_get_contents($url);
$xml .= '</channels>';
// Create a document
$doc = new DOMDocument();
$doc->loadXML($xml);
// Iterate through channel elements
foreach($doc->getElementsByTagName('channel') as $channel) {
echo $channel->getElementsByTagName('name')->item(0)->nodeValue . PHP_EOL;
echo $channel->getElementsByTagName('id')->item(0)->nodeValue . PHP_EOL;
// And so on ...
}

parsing xml file in php [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have an xml file which consist of name of the country and its code.
<country>
<name>ALBANIA</name>
<code>AL</code>
</country>
<country>
<name>ALGERIA</name>
<code>DZ</code>
</country>
<country>
<name>AMERICAN SAMOA</name>
<code>AS</code>
</country>
now I am using following php code to store them in array and printing them(country.xml file is in the same folder as this php code.
$countries = array();
$file = new SimpleXMLElement(__DIR__ . '/country.xml', null, true);
foreach ($file->country as $country) {
$name = trim($country['name']);
$code = trim(strtoupper($country['code']));
$countries[$code] = $name;
echo $code;
}
but this php code shows blank page. Can anyone guide me where I am making mistake and help me to correct it or give some better method to parse xml file.
The simplexml_load_file() in PHP will do the job.
<?php
$xml = simplexml_load_file('country.xml');
$i=0;
$countryName=array();
$countryCode=array();
foreach($xml as $k=>$v)
{
$countryName[$i] = (string) $xml->country[$i]->name;
$countryCode[$i] = (string) $xml->country[$i]->code;
$i++;
}
print_r($countryName);
print_r($countryCode);
?>

Linked in xml response to php variables [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
i am getting this result from my linked in connect script,
<person>
<email-address>xzenia1#gmail.com</email-address>
<picture-url>http://m3.licdn.com/mpr/mprx/0_UiHHf6SiF4yuBerHUkfUfkshFpomUIrHMbpBf5Iy4sOYk7FecL4XTLxtdAEl42AXsho9hGzDtRBl</picture-url>
</person>
this is the php call
$xml_response = $linkedin->getProfile("~:(email-address,picture-url)");
how to make them assign to separate PHP variable.
You can load your xml as string with simplexml_load_string and then loop in it to get all data
$xml = simplexml_load_string($xml_response);
foreach($xml as $key => $val)
{
echo "$key=>$val<br>" . "\n";
}
This will output
email-address=>xzenia1#gmail.com
picture-url=>http://m3.licdn.com/mpr/mprx/0_UiHHf6SiF4yuBerHUkfUfkshFpomUIrHMbpBf5Iy4sOYk7FecL4XTLxtdAEl42AXsho9hGzDtRBl
Live sample
Try,
$xml = (array)simplexml_load_string($xml_response);
echo $email=$xml['email-address'];
echo $picture=$xml['picture-url'];
$xml = simplexml_load_string($linkedin->getProfile("~:(email-address,picture-url)"));
echo $xml->{'email-address'}[0] . "<br />";
echo $xml->{'picture-url'}[0];
simplexmldoesn't like - in node names, therefore use $xml->{'email-address'} instead of $xml->email-address.
use index [0] on both nodes, just in case, if one day your simplexml object would contain more than one <person> node...
see it working: http://codepad.viper-7.com/dQQ6sa

Categories