I have the following xml to be parsed.
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[rel] => http://schemas.google.com/g/2005#other
[address] => xyz#gmail.com
[primary] => true
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[rel] => http://schemas.google.com/g/2005#other
[address] => abc#gmail.com
[primary] => true
)
)
)
I have this above xml and I need to get only adress from this xml.
foreach ($result as $title) {
$email[$count++]=$title->attributes()->address->__toString;
}
debug($email);
The result is this. But I want only address . need some help.
Array
(
[0] => SimpleXMLElement Object
(
)
[1] => SimpleXMLElement Object
(
)
)
see : http://www.php.net/manual/en/simplexmlelement.attributes.php
Return Values
Returns a SimpleXMLElement object that can be iterated over to loop through the attributes on the tag.
the solution is to cast the value into string,
for example :
$email[$count++]=(string)$title->attributes()->address;
Or iterate the return value will work as well
eg:
foreach($title->attributes() as $key => $val)
{
if ($key == 'address') $email[$count++] = $val;
}
Related
I'm trying to return the value of the typeName key inside this object (xml).
[geometricProperty] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[typeName] => localizacao // < - - - This is the value I need.
)
[Point] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ID] => swrefVgetZredeVcollX757678785
[srs] => ut_cm
)
[coordinates] => 38871,739716
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[typeName] => limite
)
[Polygon] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ID] => swrefVgeometryV08945038
[srs] => utm_23_cm
)
[outerBoundaryIs] => SimpleXMLElement Object
(
[LinearRing] => SimpleXMLElement Object
(
[coordinates] => 318950,7399585 31981,39650 31826,73990 316956,79750
)
)
)
)
...
I've tried the following but it returns NULL
echo "<p>This object has ".(count($n->Feature->geometricProperty))." items.</p>";
$c=0;
foreach($n->Feature->geometricProperty as $k){
$c++;
echo "<p>$c)".(gettype($k))."</p>";
foreach(array_keys(get_object_vars($k)) as $o){
echo "<p>$o</p>";
switch($o){
case "#attributes": var_dump($k->{$o}["typeName"]); $tipo=$k->{$o}["typeName"]; break;
case "Point": $value=$k->{$o}->coordinates; break;
case "Polygon": $value=$k->{$o}->outerBoundaryIs->LinearRing->coordinates; break;
case "Line...": break;
}
}
echo "<p>TIPO: $tipo</p><p>VALOR: $value</p>";
}
OUTPUT:
This object has 3 items.
1)object
#attributes
NULL
Point
TIPO:
VALOR: 31869871,739796106
2)object
#attributes
NULL
Polygon
TIPO:
Search and found this overflow question and can't guess what am I doing wrong.
You can use SimpleXmlElement::attributes() to get an array of attributes. So then you would just loop over them and display them or use implode or something... depending on the output you want.
I am pulling data from an external site using simplexml_load_file and need to dump into my local database. I do not know how to parse the data so that I can perform my sql insert. Please tell me how to grab the column headings and values from the simplexml_load_file results. Because I may not always know the heading names, Ideally I would like to pull the heading and value in as variables and load them both into sql as values to two columns; column_nam and column_value. Below is an example of the data usnig the print_r
SimpleXMLElement Object
(
[#attributes] => Array
(
[sessionid] => p_panda143ccfcf692ede95e|43413dc74e459ecb|41b5f89600000000|41c18afc4b000000|
)
[OBJECT] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => clinicalreport
[op] => search_filedelivery
)
[reports_viewed] => SimpleXMLElement Object
(
)
[reports_delivered] => SimpleXMLElement Object
(
)
[clinicalreport] => 3006163008
[orders_transmitted] => SimpleXMLElement Object
(
)
[report_request_date] => 10/7/2014 9:17 AM
[reports_undelivered] => SimpleXMLElement Object
(
)
[person_middle_name] => R
[reports_printed] => SimpleXMLElement Object
(
)
[creation_date] => SimpleXMLElement Object
(
)
[ownerid] => 3003154010
[reports_count] => SimpleXMLElement Object
(
)
[sponsor_name] => LabCorp Birmingham
[receivingorganization] => 3003154010
[organization_name] => SimpleXMLElement Object
(
)
[sponsor] => 1502182
[document] => SimpleXMLElement Object
(
)
[clearance] => 10
[report_subject] => HL7 Order Copy
[report_service_date] => 10/7/2014 9:19 AM
[reports_forwarded] => SimpleXMLElement Object
(
)
[forwarded] => SimpleXMLElement Object
(
)
[std_orders_created] => SimpleXMLElement Object
(
)
[person_last_name] => Angles
[is_viewed] => SimpleXMLElement Object
(
)
[is_latest] => SimpleXMLElement Object
(
)
[is_abnormal] => SimpleXMLElement Object
(
)
[expiration_date] => 4/5/2015 9:19 AM
[sequence] => SimpleXMLElement Object
(
)
[is_annotated] => SimpleXMLElement Object
(
)
[originalreport] => SimpleXMLElement Object
(
)
[person_account_number] => 1799
[filler_order_number] => SimpleXMLElement Object
(
)
[placer_order_number] => SimpleXMLElement Object
(
)
[is_forwarded] => SimpleXMLElement Object
(
)
[report_priority] => SimpleXMLElement Object
(
)
[person] => SimpleXMLElement Object
(
)
[receiving_cg_fname] => SimpleXMLElement Object
(
)
[creation_datetime] => 10/7/2014 9:19 AM
[receiving_cg_lname] => SimpleXMLElement Object
(
)
[is_downloaded] => N
[receiving_cg_mname] => SimpleXMLElement Object
(
)
[report_status] => NA
[lab_code] => MB
[ordering_cg_suffix] => SimpleXMLElement Object
(
)
[ordering_cg_fname] => SimpleXMLElement Object
(
)
[ordering_client_id] => 8001631
[person_first_name] => Madeleine
[report_type] => ORDER
[ordering_cg_lname] => SimpleXMLElement Object
(
)
[ordering_cg_mname] => SimpleXMLElement Object
(
)
[previousreport] => SimpleXMLElement Object
(
)
[orderresult] => SimpleXMLElement Object
(
)
[receiving_cg_suffix] => SimpleXMLElement Object
(
)
[psc_orders_created] => SimpleXMLElement Object
(
)
[receiving_client_id] => 8001631
[is_printed] => SimpleXMLElement Object
(
)
[content_expiration_date] => 2/4/2015 9:19 AM
[person_suffix] => SimpleXMLElement Object
(
)
[abnormals_count] => SimpleXMLElement Object
(
)
)
)
I found the answer. But first why is there a '-1' by my question.
SOLUTION - I needed to send the results of the simplexml_load_file in the new function xml2array. this function provided me with each name and value.
$myXML = simplexml_load_file($url);
function xml2array($myXML) {
$arr = array();
foreach ($myXML as $element) {
$tag = $element->getName();
$e = get_object_vars($element);
if (!empty($e)) {
$arr[$tag] = $element instanceof SimpleXMLElement ? xml2array($element) : $e;
}
else {
$arr[$tag] = trim($element);
}
}
return $arr;
}
I found the function here
http://bookofzeus.com/articles/convert-simplexml-object-into-php-array/
AI have an XML which have attributes as well as values in them. I want to convert it to an Array or Array Object along with attributes and values.
XML
<?xml version="1.0" encoding="UTF-8"?>
<itemBody>
<div label="options">
<optionchoices optionIdentifier="RESPONSE" shuffle="false" maxOptions="1">
<choice identifier="A1"><![CDATA[aaaa]]></choice>
<choice identifier="A2"><![CDATA[bbbb]]></choice>
<choice identifier="A3"><![CDATA[cccc]]></choice>
</optionchoices>
</div>
</itemBody>
I tried two set of code but the result was not as expected.
Code 1
<?php
$xml = simplexml_load_file('test.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
echo "<pre>";print_r($xml);echo "</pre>"; exit;
?>
Output
SimpleXMLElement Object
(
[div] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => options
)
[optionchoices] => SimpleXMLElement Object
(
[#attributes] => Array
(
[optionIdentifier] => RESPONSE
[shuffle] => false
[maxOptions] => 1
)
[choice] => Array
(
[0] => aaaa
[1] => bbbb
[2] => cccc
)
)
)
)
In the above output if we check then in choice node we get the values only and not the attributes
Code 2
<?php
$xml = simplexml_load_file('test.xml');
echo "<pre>";print_r($xml);echo "</pre>"; exit;
?>
Output
SimpleXMLElement Object
(
[div] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => options
)
[optionchoices] => SimpleXMLElement Object
(
[#attributes] => Array
(
[optionIdentifier] => RESPONSE
[shuffle] => false
[maxOptions] => 1
)
[choice] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A1
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A2
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A3
)
)
)
)
)
)
In this output we get only attributes of XML.
Now what I want is to obtain Attributes as well as Values of the XML.
Please help.
Thanks in advance.
This is what I got. And this is the solution which I expected.
http://outlandish.com/blog/xml-to-json/
I am using php to consume a web service (in coldfusion) to validate against the active directory. My code is below.
<?php
$racf = $_SERVER['AUTH_USER'];
//echo $racf;
//echo $myracf = trim($racf, "FEDERATED\.");
//get authenticated user
$arrUser = explode("\\", $_SERVER["LOGON_USER"]);
$racf = $arrUser[1];
echo $racf.'<br ><br >';
$logins = "http://acoldfusionwebservice/login.cfc?method=loginad&racf=$racf";
if( ! $xml = simplexml_load_file($logins) )
{
echo 'unable to load XML file';
}
else
{
//echo 'XML file loaded successfully <br />';
print_r ($xml);
}
?>
And this produces the following.
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 1.0
)
[header] => SimpleXMLElement Object
(
)
[data] => SimpleXMLElement Object
(
[recordset] => SimpleXMLElement Object
(
[#attributes] => Array
(
[rowCount] => 1
[fieldNames] => cn,mail,givenName,sn
)
[field] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => cn
)
[string] => B000000
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => mail
)
[string] => john.doe#company.com
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => givenName
)
[string] => John
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => sn
)
[string] => Doe
)
)
)
)
)
Can someone help me to parse this information so that I can assign variables and use them. Thanks.
Try
$xml->data->recordset->field[0]->string
I think that field[0] is an Object again
I am parsing the array given below. Looping through td using foreach. Now i want to select the value other than [#attributes]. I cannot use a or p specifically as they change through out the objects.
How can i achieve this?
[0] => SimpleXMLElement Object
(
[th] => SimpleXMLElement Object
(
[#attributes] => Array
(
[rowspan] => 2
[scope] => row
)
[p] => Memory
)
[td] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => ttl
)
[a] => Card slot
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => nfo
)
[p] => No
)
)
)
Want the solution to work in php.
Try below one
<?php
foreach($td as $element)
{
foreach($element as $key => $value)
{
if(!preg_match("/#/", $key) && !is_array($value))
echo $element[$key];
}
}
?>