This question already has answers here:
How to retrieve data from # xml attribute in PHP
(2 answers)
Closed 8 years ago.
I saw lot of examples but nothing is working perfectly. This is the array i got after parsing.
SimpleXMLElement Object
(
[#attributes] => Array
(
[areaUnits] => acre
)
)
Now i try to get attributes,like this:
var_dump($list->attributes());
I got this error:
var_dump(): Node no longer exists
<?php
function xml_attribute($object, $attribute)
{
if(isset($object[$attribute]))
return (string) $object[$attribute];
}
print xml_attribute($xml, 'areaUnits'); //prints "acre"
?>
Obtaining the attributes of a SimpleXMLElement is very straight forward.
The XML:
<?xml version="1.0"?>
<root>
<node attribute1="value1" attribute2="value2">data</node>
</root>
The PHP:
// assume $xml variable contains the XML document above
$sxe = new SimpleXMLElement($xml)
$value1 = $sxe->node->attributes()->attribute1;
$value2 = $sxe->node->attributes()->attribute2;
In your example above, $list MUST reference an actual XML node in order for you to attempt to access its attributes. Based on your error, it sounds like you're not doing that, which can often happen if you modify the XML structure referenced by $list at run time.
Related
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
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
I am parsing XML strings using simplexml_load_string(), but I noticed that i don't get the name of the very first tag.
For example, I have these two xml strings:
$s = '<?xml version="1.0" encoding="UTF-8"?>
<ParentTypeABC>
<chidren1>
<children2>1000</children2>
</chidren1>
</ParentTypeABC>
';
$t = '<?xml version="1.0" encoding="UTF-8"?>
<ParentTypeDEF>
<chidren1>
<children2>1000</children2>
</chidren1>
</ParentTypeDEF>
';
NOTICE that they are nearly identical, the only difference being that one has the first node as <ParentTypeABC> and the other as <ParentTypeDEF>
then I just convert them to SimpleXML objects:
$o = simplexml_load_string($s);
$p = simplexml_load_string($t);
but then i have two equal objects, none of them having the "top" node's name appearing, either ParentTypeABC or ParentTypeDEF (I examine the objects using print_r()):
// with top node "ParentTypeABC"
SimpleXMLElement Object
(
[chidren1] => SimpleXMLElement Object
(
[children2] => 1000
)
)
// with top node "ParentTypeDEF"
SimpleXMLElement Object
(
[chidren1] => SimpleXMLElement Object
(
[children2] => 1000
)
)
So how I am supposed to know the top node's name? If I parse unknown XMLs and I need to know what's the top node name, what can I do?
Is there an option in simplexml_load_string() I could use?
I know there are MANY ways to parse XML's with PHP, but I'd like it to be as simple as posible, and to get a simple object or array I could navigate easily.
I made a simple example here to fiddle with.
SimpleXML has a getName() method.
echo $xml->getName();
This should return the name of the respective node, no matter if root or not.
http://php.net/manual/en/simplexmlelement.getname.php
This question already has answers here:
SimpleXML: Selecting Elements Which Have A Certain Attribute Value
(2 answers)
Closed 8 years ago.
I have XML-file and i try to get value. I need value 12345 from variable media_id. How i can get it with php and simplexml?
<?xml version="1.0" encoding="UTF-8"?>
<Playerdata>
<Clip>
<MediaType>video_episode</MediaType>
<Duration>5400</Duration>
<PassthroughVariables>
<variable name="media_type" value="video_episode"/>
<variable name="media_id" value="12345"/>
</PassthroughVariables>
</Clip>
</Playerdata>
I have now only:
$xml = simplexml_load_file("file.xml");
Try this:
$xml = simplexml_load_file("file.xml");
$variable = $xml->xpath('//variable[#name="media_id"]')[0];
echo $variable["value"];
You can load your XML file into Simplexml which will parse it and return an SimpleXML object.
$xml = simplexml_load_file('path/to/file.xml');
//then you should be able to access the data through objects
$passthrough = $xml->Clip->PassthroughVariables;
//because you have many children in the PassthroughVariables you'll need to iterate
foreach($passthrough as $p){
//to get the attributes of each node you'll have to call attributes() on the object
$attributes = $p->attributes();
//now we can iterate over each attribute
foreach($attributes as $a){
//SimpleXML will assume each data type is a SimpleXMLElement/Node
//so we need to cast it for comparisons
if((String)$a->name == "media_id"){
return (int)$a->value;
}
}
}
The SimpleXMLElement documentation is probably a good starting point when it comes to working with the SimpleXMLObject. http://uk1.php.net/manual/en/class.simplexmlelement.php
Here is w/o Xpath
$xml = simplexml_load_file('file.xml');
$value = (int) $xml->Clip->PassthroughVariables->variable[1]['value'];
This question already has answers here:
Remove a child with a specific attribute, in SimpleXML for PHP
(18 answers)
Closed 8 years ago.
I have an xml file of this structure
<?xml version="1.0" encoding="iso-8859-1"?>
<my_events>
<event id="e20111129215359">
<title>the title</title>
<channel id="1">
<name>A name</name>
<onclick></onclick>
</channel>
<event_site>
<name/>
<url/>
</event_site>
<start_date>Thu Mar 08 2012</start_date>
<start_time>11:00 AM</start_time>
<end_date>null</end_date>
<end_time>null</end_time>
<notes>Notes for the event</notes>
</event>
</my_events>
To delete an event, I have this php function.
<?php
include_once("phpshared.php");
function delete_event( $nodeid ) {
$nodes = new SimpleXMLElement('my_events.xml', LIBXML_NOCDATA, true);
$node = $nodes->xpath("/my_events/event[#id='$nodeid']");
$node->parentNode->removeChild($node);
$formatted = formatXmlString($nodes->asXML());
$file = fopen ('my_events.xml', "w");
fwrite($file, $formatted);
fclose ($file);
}
echo delete_event(trim($_REQUEST['nodeid']));
?>
That doesn't delete the node. Is there a different way to do this?
SimpleXML allows removal of elements via PHP's unset() keyword.
For your code snippet, simply replace
$node->parentNode->removeChild($node);
with
if ( ! empty($node)) {
unset($node[0][0]);
}
If the XPath query returned a matching <event> element, we instruct SimpleXML to unset() it.
Aside: here are two occurrences of [0] because:
xpath() returns an array, even if only one element matches. So [0] is used to get the first item in that array, which is the element we want to delete.
The SimpleXMLElement returned from $node[0] represents a collection of <event> elements (but if you access elements/attributes on it then the values from the first in the collection is used). So, we use [0] to get at the actual SimpleXMLElement that we want to delete, which is the first in this magical collection.
Use unset(): Remove a child with a specific attribute, in SimpleXML for PHP