How can I extract the value from this SimpleXml? I keep getting an empty array, and I don't know what I'm doing wrong.
I just want to extract the string "Familial GI Stromal Tumor With Loss of Heterozygosity and Amplification of Mutant KIT.".
object(SimpleXMLElement)#13 (2) {
["#attributes"]=>
array(2) {
["Name"]=>
string(5) "Title"
["Type"]=>
string(6) "String"
}
[0]=>
string(86) "Familial GI Stromal Tumor With Loss of Heterozygosity and Amplification of Mutant KIT."
}
SimpleXMLElement has a __toString() method. For the element you showed in your question, you should be able to just echo the string content.
echo $yourElement;
or if you want it in a variable, you can call __toString() explicitly
$someVar = $yourElement->__toString();
or trigger it by treating the element as a string;
$someVar = "$yourElement";
Just convert it into a string.
var_dump((string) $element);
Anyway, it depends on your xpath.
Thanks for the help, everyone. Turns out I figured it out. I simply had to access it from the array.
$title = $pub->xpath('Item[#Name="Title"]')[0];
Related
I have an object being passed into a function which I have no control over and it is in the below format, with the root being 'entity'.
object(SimpleXMLElement)#6 (1) { ["#attributes"]=> array(2) { ["id"]=> string(2) "12" ["name"]=> string(17) "Test Object Value" } }
Now I'm trying to pull out just the name by using both the below snippets but both output empty values.
entity[0]->name;
and
entity->{'#attributes'}->name;
Is there a special way to deal with characters in element names when the curly brackets format doesn't work?
You need to use attribute() function for getting the attributes in a simpleXML object. Your code should be something like:
$parsed = $simplexmlObject->entity->attribute()->desiredProperty;
Update: Got this technique from a question asked from me, How to parse value `#attribute` from a SimpleXMLObject in PHP
You can get the name attribute as follows:
$name = $entity->attributes()->name;
echo $name;
I think theres something more going on here but it looks like certain data available in the raw xml is unavailable to me after it gets set in the simplexml element via my soap call.
Here's the raw information I get back from the soap call (retrieved w/ soapUI), Please note the e_mail element in particular.
<GetSubscriberData_ByDrupal_IdResult><![CDATA[
<SubscriberDataRoot>
<SubscriberData>
<city>MEDIA</city>
<state>PA</state>
<zip>19063-4112</zip>
<country>USA</country>
<phone>1231231244</phone>
<e_mail>some_email#somewhere.com</e_mail>
</SubscriberData>
</SubscriberDataRoot>]]>
</GetSubscriberData_ByDrupal_IdResult>
and here is the SimpleXMLElement that I var_dump'd after it was yielded from my SoapClient call.
object(SimpleXMLElement)#26 (1) {
["SubscriberData"]=>
object(SimpleXMLElement)#33 (29) {
["city"]=>
string(5) "MEDIA"
["state"]=>
string(2) "PA"
["zip"]=>
string(10) "19063-4112"
["country"]=>
string(3) "USA"
["phone"]=>
string(10) "1231231234"
["e_mail"]=>
object(SimpleXMLElement)#36 (0) {
}
}
}
I can access most of the data as expected with something akin to $soap_data->SubscriberData->city however notable the e_mail element is not available directly, its another SimpleXMLElement. I've tried iterating over it, using asXML, __toString, casting to a (string)..
(for instance...)
php> var_dump($acct->SubscriberData->e_mail);
object(SimpleXMLElement)#38 (0) {
}
php> var_dump($acct->SubscriberData->e_mail->asXML());
string(17) "<e_mail></e_mail>"
php> var_dump($acct->SubscriberData->e_mail->__toString());
string(0) ""
php> var_dump((string) $acct->SubscriberData->e_mail);
string(0) ""
but I can't access it like any of the other string valued elements. I want to be able to get the string value such as $email = $acct->SubscriberData->e_mail like the rest of the elements. Thanks in advance.
I'm not sure I can test/reproduce your object structure without your soap stuff, but if I took the raw XML and tried to process it, I'd have to process the CDATA block separately.
$GetSubscriberData_ByDrupal_IdResult = simplexml_load_string($xmlSource);
$SubscriberDataRoot = simplexml_load_string((string) $GetSubscriberData_ByDrupal_IdResult);
$email = (string) $SubscriberDataRoot->SubscriberData->e_mail;
echo $email; // assigned to var just for demonstration
I'm pulling data from a Blogger RSS feed. I have most of it narrowed down how I would like it to be, except for one thing. In the following object, how would I get the string in the term section? I've tried about every syntax I can think of, but I honestly have run out of ideas.
object(SimpleXMLElement)#7 (1) {
["#attributes"]=>
array(2) {
["scheme"]=>
string(31) "http://www.blogger.com/atom/ns#"
["term"]=>
string(7) "happens"
}
}
I've tried to var_dump $item->attributesand $item->#attributes with no luck.
Use the attributes() method:
$atts = $xml->attributes();
echo $atts['term'];
Alternatively, you could also use:
$xml->attributes()->{'term'};
var_dump($object->{'#attributes'}['term']);
or
$tmp = '#attributes';
var_dump($object->{$tmp}['term']);
i passed a json_encoded array to javascript. Now i would like to acces that array to get the different elemtnts.
i print it out in the console.log() and i get this array:
array(1) {
[16]=>
array(2) {
[3488]=>
array(1) {
[0]=>
array(2) {
["article_no_internal"]=>
string(6) "999184"
["article_name_internal"]=>
string(29) "Geschenkbox Kerzenschein 2011"
}
}
[2615]=>
array(1) {
[0]=>
array(2) {
["article_no_internal"]=>
string(6) "700469"
["article_name_internal"]=>
string(29) "Hotelscheck RomantischeTagef2"
}
}
}
}
This is about right. How can i access the article_name of the second array, with the ID 2615?
found a related question here reading a jsone object, hope for some better explebation or answer. Thanks.
EDIT:
As it seems i made a mistake, i showed a php var_dump in the console. When i try to show the javascript array in the console i get undefined.
Since JSON means "JavaScript Object Notation" you don't need to do anything to access the object's items.
For example you can access:
jsonObject[2615][0]["article_name_internal"]
if this object is String, use eval to convert the string to a JavaScript object and access the items in the same way with the previous example.
var jsonObject = eval(jsonstring);
jsonObject[2615][0]["article_name_internal"]
After an xPath, I'm left with this var_dump:
array(1) {
[0]=>
object(SimpleXMLElement)#2 (1) {
[0]=>
string(11) "22-99586795"
}
}
echoing the damn thing only gives me "Array()"
How do I get the bloody string out?
Thanks
It's an array with one item, so you need to do:
$myelement[0];
or
$myelement[0][0];
(I can't tell from your question which element you're referring to)
Try casting it to string
print (string)$yourarray[0];