How to get attribute value from element with several attributes - php

I have this xml file (part of it):
<exchangerates xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" type="Valutakurser" author="Danmarks Nationalbank" refcur="DKK" refamt="1">
<dailyrates id="2020-10-23">
<currency code="AUD" desc="Australske dollar" rate="448,83"/>
<currency code="BGN" desc="Bulgarske lev" rate="380,44"/>
I want to get the rate from AUD, Australian dollar.
My xpath query looks like this:
$result = $xml->xpath("/exchangerates/dailyrates/currency[#code = 'AUD']");
It works and deliver this output:
Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array ( [code] => AUD [desc] => Australske
dollar [rate] => 448,83 ) ) )
And now I am stuck. I want the [rate] value. But how? I have solved this in c# using a navigator.
But now I am using php. I could use javascript if necessary.

From the xpath point of view, you just need to add /#attributeName to your xpath.
/exchangerates/dailyrates/currency[#code = 'AUD']/#rate

Related

Accessing specific data from a cURL response in PHP

If anyone can help here it would be very much appreciated. I have created a function which sends a cURL request and saves the response. For a specific request I have received the following response. Which when I use print_r gives...
A string:
2017-08-25T09:56:29.172ZSuccess949E949_INTL_API_17934825_R1TESTUSB_WHITETESTUSB_BLACK
And some XML:
<getitemresponse xmlns="urn:ebay:apis:eBLBaseComponents">
<timestamp>2017-08-25T09:56:29.172Z</timestamp>
<ack>Success</ack>
<version>949</version>
<build>E949_INTL_API_17934825_R1</build>
<item>
<variations>
<variation>
<sku>TESTUSB_WHITE</sku>
</variation>
<variation>
<sku>TESTUSB_BLACK</sku>
</variation>
</variations>
</item>
</getitemresponse>
My question is how do I access the values at the sku elements.
I have tried to convert the response to a \SimpleXmlElement and used print_r which gives.
SimpleXMLElement Object
(
[Timestamp] => 2017-08-25T10:09:59.016Z
[Ack] => Success
[Version] => 949
[Build] => E949_INTL_API_17934825_R1
[Item] => SimpleXMLElement Object
(
[Variations] => SimpleXMLElement Object
(
[Variation] => Array
(
[0] => SimpleXMLElement Object
(
[SKU] => TESTUSB_WHITE
)
[1] => SimpleXMLElement Object
(
[SKU] => TESTUSB_BLACK
)
)
)
)
)
And again I don't know how to access SKU here. Any help would be very much appreciated thanks for reading. Also if you have a different approach all together I would also be happy to hear that. Can I access SKU directly from the string response?
you can use XPath to access this node-values:
$xml = new \SimpleXmlElement($xmlAsString);
$xml->registerXPathNamespace('ebay', 'urn:ebay:apis:eBLBaseComponents');
$skuNodes = $xml->xpath('//ebay:sku'); // return all sku elements, everywhere in the document
now you can loop through them or do what ever you want
foreach($skuNodes as $_ => $node)
echo "<span>".htmlentities($node)."</span>";
fiddle: http://codepad.org/tzSvo4yV
Updated Try this..
$xmlArray = simplexml_load_string($xml);
$json = json_encode($xmlArray);
$xmlFormated = json_decode($json,TRUE);
Now, you can loop through array to find all sku elements.
foreach($xmlFormated['item']['variations']['variation'] as $key => $value) {
echo $key.' '.$value['sku'];
}

SimpleXML to Array

I load some XML which produces a SimpleXML Object like so (only displaying one Object)
SimpleXMLElement Object
(
[Jobs] => SimpleXMLElement Object
(
[Job] => Array
(
[0] => SimpleXMLElement Object
(
[ID] => J000001
[Name] => Internal Time
[Description] => Use this job to record your internal and non-billable time for activities such as annual leave, sick leave, professional development, staff meetings etc
[Client] => SimpleXMLElement Object
(
[ID] => 8430219
[Name] => Fake Client
)
[ClientOrderNumber] => SimpleXMLElement Object
(
)
[State] => Planned
[StartDate] => 2016-03-21T00:00:00
[DueDate] => 2017-03-21T00:00:00
[InternalID] => 11442733
[Assigned] => SimpleXMLElement Object
(
[Staff] => SimpleXMLElement Object
(
[ID] => 344460
[Name] => Som Name
)
)
)
)
)
)
What I would like to do is create an array of the client Name and client ID. So I am aiming for something like so
[data] => array (
8430219 => Fake Client,
8430343 => Another Client,
etc
)
At the moment, I can get the name in place, but struggling with the id because it says it is an illegal offset type. This is what I have
foreach($oXML->Jobs as $oEntry) {
foreach ($oEntry->Job as $data) {
$jobsArray = array(
$data->Client->ID => $data->Client->Name
);
}
}
How can I create the array based on the SimpleXML Object I have?
Thanks
First of all, to obtain your desired array you can not use this syntax:
$jobsArray = array( $key => $val );
You have to use something like this:
$jobsArray[$key] = $val;
Otherwise, at each repeating loop, your syntax will override precedent array values.
Then, I suggest you to use XPath to simplify entire process. With XPath queries, you can retrieve a set of nodes with only one search.
Assuming you have this XML:
<?xml version="1.0"?>
<Jobs>
<Job>
<Client>
<ID>8430219</ID>
<Name>Fake Client</Name>
</Client>
<Client>
<ID>8430220</ID>
<Name>Fake Client 2</Name>
</Client>
</Job>
<Job>
<Client>
<ID>8430221</ID>
<Name>Fake Client 3</Name>
</Client>
</Job>
</Jobs>
With this xpath query:
$clients = $oXML->xpath( '/Jobs/Job/Client' );
you obtain in $clients all <Client> nodes. Then you can create your array in this way:
$result = array();
foreach( $clients as $client )
{
$result[$client->ID->__toString()] = $client->Name->__toString();
}
This is $result after foreach loop:
Array
(
[8430219] => Fake Client
[8430220] => Fake Client 2
[8430221] => Fake Client 3
)
Note that we have to cast as string single nodes (that are SimpleXML objects): this is absolutely necessary creating keys, otherwise the array assignment fails. You can obtain same result using (string) $client->ID instead of $client->ID->__toString()
Read more about SimpleXMLElement::xpath
Read more about XPath

PHP SimpleXML xpath query results

I have searched for this and the answers I find seem to say what I thought I understand. Obviously I am missing something. I am confused at the results from the xPath query. I have simplified my problem for a test case to post here.
My real xml has several dataset nodes at different depths. Ultimately, I want to get every dataset element with a given label and then loop over that and get the field values (at different locations (or depths) so I think I need xpath). I can use xpath to get the dataset elements that I want successfully. However, when I then run xpath on that result object, it gets me the fields I want and all the other fields too. I can't figure out why it isn't only returning field1, field2, and field3. When I print_r($value[0]), it shows only the fields I want. But, when I run xpath on $value[0], it returns all fields in the xml doc.
Sample XML
<myxml>
<dataset label="wanteddata" label2="anotherlabel">
<dataitem>
<textdata>
<field label="label1">field1</field>
<field label="label2">field2</field>
<field label="label3">field3</field>
</textdata>
</dataitem>
</dataset>
<dataset label="unwanteddata" label2="unwantedanotherlabel">
<dataitem>
<textdata>
<field label="label4">field4</field>
<field label="label5">field5</field>
<field label="label6">field6</field>
</textdata>
</dataitem>
</dataset>
</myxml>
Here is the test code.
$xmlstring = file_get_contents('simplexml_test.xml');
$xml = simplexml_load_string($xmlstring);
if ($xml === false) {
throw new Exception("Failed to load");
}
$value = $xml->xpath('//dataset[#label="wanteddata"]');
print_r($value[0]->xpath('//field'));
Code Output:
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => label1
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => label2
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => label3
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => label4
)
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => label5
)
)
[5] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => label6
)
)
)
//field selects all <field> elements within the entire XML document regardless of the context node from which you call that XPath. To make the XPath heed the context node, you need to add a dot (.) at the beginning of the XPath. In XPath, (.) references current context node :
print_r($value[0]->xpath('.//field'));

access data attributes using simpleXML

I am using an API to get a Block FIPS number but I have not been able to target that specific number within the XML file.
I did a print_r() on the xml output and here is what I get
SimpleXMLElement Object ( [#attributes] => Array ( [status] => OK [executionTime] => 6 ) [Block] => SimpleXMLElement Object ( [#attributes] => Array ( [FIPS] => 060730200252015 ) ) [County] => SimpleXMLElement Object ( [#attributes] => Array ( [FIPS] => 06073 [name] => San Diego ) ) [State] => SimpleXMLElement Object ( [#attributes] => Array ( [FIPS] => 06 [code] => CA [name] => California ) ) )
Here is the XML that is being generated
<Response xmlns="http://data.fcc.gov/api" status="OK" executionTime="10">
<Block FIPS="060730200252015"/>
<County FIPS="06073" name="San Diego"/>
<State FIPS="06" code="CA" name="California"/>
</Response>
I have been trying to get the Block FIPS Number like this:
$fccAPI = "http://data.fcc.gov/api/block/2010/find?latitude=$lat&longitude=$lng";
//echo $fccAPI;
$fccXML= simplexml_load_file($fccAPI);
print_r($fccXML);
//Echo FIPS Number
echo $fccXML->FIPS;
Please help me target the Block FIPS number.
You need to use the following:
echo $fccXML->Block[0]['FIPS'];
$fccXML is the root node, the <Response> element. ->Block[0] selects the first Block element, and to access an attribute, use the square brackets notation with the attribute name, i.e. ['FIPS'].
The SimpleXML documentation has numerous examples if you're having trouble with the syntax.

XML ATOM to SimpleXML Object losing data

Hi, i have a simple problem. When i try to convert an atom XML string to simplexml object or using simplexml_load_data function, it seems to drop many attributes data..
like in my original string i have
<?xml version="1.0" encoding="UTF-8"?>
<feed gd:kind="customsearch#search" xmlns="http://www.w3.org/2005/Atom" xmlns:cse="http://schemas.google.com/cseapi/2010" xmlns:gd="http://schemas.google.com/g/2005" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
<title>Google Custom Search - flower</title>
<id>tag:www.googleapis.com,2010-09-29:/customsearch/v1?q=flower&&num=10&start=1&safe=off</id>
<author>
<name>- Google Custom Search</name>
</author>
<updated>1970-01-16T10:55:22.093Z</updated>
<opensearch:Url type="application/atom+xml" template="https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={cse:safe?}&cx={cse:cx?}&cref={cse:cref?}&sort={cse:sort?}&filter={cse:filter?}&gl={cse:gl?}&cr={cse:cr?}}&googlehost={cse:googleHost?}&c2coff={?cse:disableCnTwTranslation}&hq={cse:hq?}&hl={cse:hl?}&siteSearch={cse:siteSearch?}&siteSearchFilter={cse:siteSearchFilter?}&exactTerms={cse:exactTerms?}&excludeTerms={cse:excludeTerms?}&linkSite={cse:linkSite?}&orTerms={cse:orTerms?}&relatedSite={cse:relatedSite?}&dateRestrict={cse:dateRestrict?}&lowRange={cse:lowRange?}&highRange={cse:highRange?}&searchType={cse:searchType?}&fileType={cse:fileType?}&rights={cse:rights?}&imgsz={cse:imgsz?}&imgtype={cse:imgtype?}&imgc={cse:imgc?}&imgcolor={cse:imgcolor?}&alt=atom"/>
<opensearch:Query role="request" title="Google Custom Search - flower" totalResults="98800000" searchTerms="flower" count="10" startIndex="1" inputEncoding="utf8" outputEncoding="utf8" cse:safe="off" cse:cx="g"/>
<opensearch:Query role="cse:nextPage" title="Google Custom Search - flower" totalResults="98800000" searchTerms="flower" count="10" startIndex="11" inputEncoding="utf8" outputEncoding="utf8" cse:safe="off" cse:cx="0vv1rbg"/>
<opensearch:totalResults>98800000</opensearch:totalResults>
<opensearch:startIndex>1</opensearch:startIndex>
but when i convert it to xml using
$xml = simplexml_load_string($rs);
print_r($xml);
my output is like below. All the key information like number of searches, total results set is gone..
SimpleXMLElement Object
(
[title] => Google Custom Search - flower
[id] => tag:www.googleapis.com,2010-09-29:/customsearch/v1?q=flower&cx=&num=10&start=1&safe=off
[author] => SimpleXMLElement Object
(
[name] => Search Engine - Google Custom Search
)
[updated] => 1970-01-16T10:55:22.093Z
[entry] => Array
(
[0] => SimpleXMLElement Object
(
[id] => http://en.wikipedia.org/wiki/Flower
[updated] => 1970-01-16T10:55:22.093Z
[title] => <b>Flower</b> - Wikipedia, the free encyclopedia
[link] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => http://en.wikipedia.org/wiki/Flower
[title] => en.wikipedia.org
)
)
[summary] => A <b>flower</b>, sometimes known as a bloom or blossom, is the reproductive structure <br> found in <b>flowering</b> plants (plants of the division Magnoliophyta, also called <b>...</b>
)
Those elements are not gone, you just can't "see" them in a standard dump, because they are in a different namespace. You can still iterate over them like this:
foreach ($xml->children('opensearch', TRUE) as $element) {
echo $element->getName(), "\n";
}
Where 'opensearch' indicates the namespace prefix and TRUE means that you used the document specific prefix instead of the full namespace url.

Categories