php SimpleXML attributes are missing
The simple answer here is not to use print_r()
with SimpleXML objects. Because they are wrappers around non-PHP data, functions like that which would normally show the "whole" object don't really reflect what you're looking at.
The way to access an attribute with SimpleXML is to use the attribute name as though it was an array key ($node['attribute']
); this does not mean that there is an array somewhere with that key, it is a function-call in disguise.
If you want to get a feel for which nodes you're looking at while writing SimpleXML code, check out this simplexml_dump()
function which I wrote (feedback welcome).
PHP SimpleXML is missing attributes
The attributes
method requires the full namespace, not just the prefix:
$s = '...';
$xml = new SimpleXMLElement($s);
$attributes = $xml->g->image->attributes('http://www.w3.org/1999/xlink');
echo $attributes['href'];
Or use the second parameter of attributes
(is_prefix
- boolean) and specify the prefix only:
$attributes = $xml->g->image->attributes('xlink', true);
Here it is in action.
Missing Attributes while parsing XML with simplexml_load_string
The <SampleData>
value as you say is encoded, the simplest way to get this back to 'normal' is to use htmlspecialchars_decode()
to convert all the symbols before loading the string into SimpleXML
. The code below does this and then outputs various parts of the data as an example of how to display information...
$source = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=biosample&id=367368";
$value = file_get_contents($source);
$value = htmlspecialchars_decode($value);
$xml = simplexml_load_string($value);
// Access the DbBuild value
echo "DbBuild=".(string)$xml->DocumentSummarySet->DbBuild.PHP_EOL;
// The BioSample publication date attribute
echo "BioSample publication date=".(string)$xml->DocumentSummarySet->DocumentSummary->SampleData->BioSample['publication_date'].PHP_EOL;
// List the attributes name and value
foreach ( $xml->DocumentSummarySet->DocumentSummary->SampleData->BioSample->Attributes->Attribute as $attribute ) {
echo (string)$attribute['attribute_name']."=".(string)$attribute.PHP_EOL;
}
Some of the XML access looks long winded, but it's just a case of accessing the various levels of data in the document. $xml->DocumentSummarySet
accesses the <DocumentSummarySet>
element under the root elements. BioSample['publication_date']
is the publication_date attribute in the <BioSample>
element and so on.
SimpleXMLElement is removing attributes (php 7.2)
Do not use any of the print_r()
or var_dump()
on a SimpleXML object, this will abbreviate the output as there is potentially a lot of it. If you want to check the document loaded use asXML()
...
echo $xmlConvertedData->asXML();
or to output the one elements language...
echo $xmlConvertedData->event[0]->details->names->translation['language'];
( You also need to correct the last element of the sample - </events>
)
php missing attributes while parsing XML
The loading of the XML works fine, but print_r
is not able to print everything from an XML object
. See this repository for a solution if you want to dump the XML code: simplexml_debug
You can access the attribute you want with
echo $output->ads->ad[0]->url->attributes();
Which works perfectly fine.
Related Topics
MySQL Select Records Greater Than 3 Months
Simplexml: Working with Xml Containing Namespaces
Difference Between Var_Dump,Var_Export & Print_R
Issues with PHP 5.3 and Sessions Folder
Sending Multiple Data Parameters with Jquery Ajax
How to Notify a User After an Access_Control Rule Redirects
PHP Mail() Attachment Problems
Bind Param with Array of Parameters
PHP Datetime() Class, Change First Day of the Week to Monday
How to Dynamically Populate Options on Dropdown Lists Based on Selection in Another Dropdown
Check If Url Has Certain String with PHP
Using MySQL Concat() in Where Clause