How to load a simplexmlobject file into a variable - php

I am going crazy here...
I already have a SimpleXMLElement Object so it is not an XML file...
I got it stored in a file object.txt
Now i want to put this object into a variable, but i can't get it to work...?
the SimpleXMLElement Object looks like this:
SimpleXMLElement Object
(
[PubmedArticle] => Array
(
[0] => SimpleXMLElement Object
(
[MedlineCitation] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Owner] => NLM
[Status] => In-Process
)
[PMID] => 20538400
[DateCreated] => SimpleXMLElement Object
(
[Year] => 2010
[Month] => 07
[Day] => 08
)
[Article] => SimpleXMLElement Object
(
[#attributes] => Array
(
[PubModel] => Print-Electronic
)
.....etc etc....
how do i put this into a variable??
i tried this:
$simplexml = file_get_contents('object.xml');
$simplexml should be an array

Loading simplexml from file: simplexml_load_file: $a = simplexml_load_file('object.xml');
If you need something else, please explain.

If your 'object.xml' file contains the above example and not actual XML, it looks a lot like you are using the results of a vardump, and you possibly wish to load that into a (or several) SimpleXMLElementObject(s)...
So, really, you have a string inside of a text file, and you want to parse out the contents. I found this: converting text file into xml using php? and thought it would be a good starting point.

Related

PHP for directory listing

i wont to list a set of files in a directory and the files in its sub directory using a loop rather than the function
as im getting the information about the directory through an xml based webdav and php native functions are infertile so please understand the issue has not yet been lodged here
$urlloc is used to remove the same directory from being looped again
foreach ($xml as $key) {
if(empty($key->propstat->prop->resourcetype[0])){
echo $key->href."<br/>";//files are printed, for debugging perposes im printing it
}else{
$Nurlloc=$key->href;
if ($Nurlloc!=$urlloc){
echo "<b>".$Nurlloc."</b><br/>";//directorys printed in bold for debugging
$urlloc=$Nurlloc;
//gtndirdown()
above is the method im getting to know if its a directory or not
NOTE i want to be able to make this code loop through and get me all the files in the directory i will also post the array of files im getting
[response] => Array
(
[0] => SimpleXMLElement Object
(
[href] => /dav/product_images/
[propstat] => SimpleXMLElement Object
(
[prop] => SimpleXMLElement Object
(
[resourcetype] => SimpleXMLElement Object
(
[0] => SimpleXMLElement Object
(
)
)
[quota-used-bytes] => 2147483647
[quota-available-bytes] => 2147483647
)
[status] => HTTP/1.1 200 OK
)
)
[1] => SimpleXMLElement Object
(
[href] => /dav/product_images/a/
[propstat] => SimpleXMLElement Object
(
[prop] => SimpleXMLElement Object
(
[resourcetype] => SimpleXMLElement Object
(
[0] => SimpleXMLElement Object
(
)
)
[quota-used-bytes] => 2147483647
[quota-available-bytes] => 2147483647
)
[status] => HTTP/1.1 200 OK
)
)
iv been stuck in this issue for 4 days and i have would like if some one could come up with a logic for this issue
maybe this is an idea that you can implement in your logic
// the name of directory
$dir='path_to_your_directory';
$files = array_slice(scandir($dir), 2);
print_r($files);

Export Array of SimpleXMLElement objects to MySQL Database

I have a SOAP Response from a Web Service and have extracted the XML data as a SimpleXMLElement. I have then iterated through this object to extract the various fields I need and save them into an array, which becomes an array of SimpleXMLElement objects.
I am now trying to export this data into a MySQL Database which, according to my research, means turning the array into a String and then using mysql_query("INSERT INTO (whatever) VALUES (whatever)");. I have tried implode and serialize but neither work and I get the error:
Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed'
This is what the array I have created from the SimpleXMLELement looks like:
Array
(
[0] => Array
(
[uid] => SimpleXMLElement Object
(
[0] => WOS:000238186400009
)
[journal] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => source
)
)
[publication] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => item
)
[0] => Abundance of hedgehogs (Erinaceus europaeus) in relation to the density and distribution of badgers (Meles meles)
)
[year] => 2006
[author1] => SimpleXMLElement Object
(
[0] => Young, RP
)
[address] => SimpleXMLElement Object
(
[0] => Cent Sci Lab, Sand Hutton, Yorks, England
)
[author2] => SimpleXMLElement Object
(
[0] => Davison, J
)
[author3] => SimpleXMLElement Object
(
[0] => Trewby, ID
)
[citations] => SimpleXMLElement Object
(
[#attributes] => Array
(
[local_count] => 15
[coll_id] => WOS
)
)
) ... etc ...
)
Can anyone help me with the method to get this data into my database, please? Do I need to change it into (yet) another format?
You have to iterate through your array to create a new array fulfilled with strings instead of SimpleXMLElement, such as :
<?php
// your array (already built)
$arraySimpleXml = array(
"example1" => new SimpleXMLElement("<test>value</test>"),
"example2" => new SimpleXMLElement("<test>value2</test>")
);
// output array, to store in database
$result = array();
foreach($arraySimpleXml as $key => $simpleXml) {
$result[$key] = $simpleXml->asXML();
}
// gets your result as a string => you can now insert it into mysql
$dbInsertion = serialize($result);
?>
So I worked out how to change the data into a standard array rather than an array of SimpleXMLElements so that I can successfully insert it into a MySQL database.
When iterating the SimpleXMLElement object to extract the data I needed I cast the type as String so that now my array has the format (as opposed to above):
Array
(
[0] => Array
(
[uid] => WOS:000238186400009
[journal] => JOURNAL OF ZOOLOGY
[publication] => Abundance of hedgehogs (Erinaceus europaeus) in relation to the density and distribution of badgers (Meles meles)
[year] => 2006
[author1] => Young, RP
[address] => Cent Sci Lab, Sand Hutton, Yorks, England
[author2] => Davison, J
[author3] => Trewby, ID
[citations] => 15
)
)
Thought I'd post this in case anyone has a similar problem in future. To do this, when iterating the data instead of:
$uid = $record->UID;
I did:
$uid = (string)$record->UID;
For each of the data fields I required. This ensures the data is stored as a String and so removes the SimpleXMLElement format.

Reading XML file - strange PHP object notation

I have a XML file simillar to this :
<information version="2">
<currentTime>2014-06-06 17:28:16</currentTime>
<result>
<name>Mark</name>
<surname>Smith</surname>
</result>
I read it with php function and parse it to the object with function, like this:
function parse_data($data){
$return_data['currentTime'] = $data->currentTime;
$return_data['name'] = $data->result->name;
$return_data['surname'] = $data->result->surname;
return $return_data;
}
$xml = simplexml_load_string(file_get_contents($link));
$object = parse_data($xml);
Then, when I echo it on the screen, to check how it look:
//json_encode($xml);
{
"#attributes":{"version":"2"},
"currentTime":"2014-06-06 17:28:16",
"result":{"name":"Mark","surname":"Smith"}
}
//print_r($xml);
SimpleXMLElement Object (
[#attributes] => Array ( [version] => 2 )
[currentTime] => 2014-06-06 17:56:30
[result] => SimpleXMLElement Object (
[name] => Mark
[surname] => Smith
)
)
//json_encode($object);
{
"currentTime":{"0":"2014-06-06 17:28:16"},
"name":{"0":"Mark"},
"surname":{"0":"Smith"}
}
//print_r($object);
Array (
[currentTime] => SimpleXMLElement Object ( [0] => 2014-06-06 17:52:50 )
[name] => SimpleXMLElement Object ( [0] => Mark)
[surname] => SimpleXMLElement Object ( [0] => Smith )
)
What is wrong with my code? He seems to read the informaton in xml file as array? Because of this strange notation I cannont operate on this data normally.
It also behave like this:
echo json_encode($object['name']); will give -> {"0":"Mark"}
echo $object['name']; will give -> Mark
Can anybody help me? What am I doing wrong?
I want my $object to look like this:
//json_encode($object);
{
"currentTime":"2014-06-06 17:28:16",
"name":"Mark",
"surname":"Smith"
}
Edit1: added print_r values
Yes, as you have noticed the type returned by $someSimpleXMLNode is an object. If you want the node value (as a string for example) use:
$return_data['currentTime'] = (string)$data->currentTime;
which is the same as doing
$return_data['currentTime'] = $data->currentTime->__toString();
etc
When you do
echo $data->currentTime;
the node is automatically coerced into a string (because echo only handles strings). This is done (generally, in php) by the object's __toString method.

php SimpleXml & arrays issue

When I print_r($var) I get the result below.
SimpleXMLElement Object
(
[SEND_FILE] => SimpleXMLElement Object
(
[FILEID] => 123
[GUID] => 456
[SUMMARY] => SimpleXMLElement Object
(
[NB_PAYMENTS] => 1
)
)
)
How can I get the value of the FILEID element in a variable? If I do
print $result->SEND_FILE->FILEID[0]
then I just get the number - what I want, no mention of a SimpleXML Object.
But if I put this variable in an array, as such
$res['file_id'] = $result->SEND_FILE->FILEID[0]
and then print_r($res) I get:
Array
(
[file_id] => SimpleXMLElement Object
(
[0] => 307466
)
)
How can I get it to remove the [0] / SimpleXMLElement Object?
This will look not too elegant, but try casting the result to integer (if the type is known):
$res['file_id'] = (int)$result->SEND_FILE->FILEID[0]
Why do you append the [0] at the end? You dont need that. You should simply do
print $result->SEND_FILE->FILEID;
And that should be enough.

how to get to a specific part of this nested array/object

This is driving me crazy, I am trying to get to a specific part of this object and it is driving me crazy, here is the object contents:
XMLHandler Object
(
[doc:XMLHandler:private] => SimpleXMLElement Object
(
[#attributes] => Array
(
[state] => Live
)
[newsListItem] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => http://api.contentplus.co.uk/6cb5ea15-d6b1-4c40-9db7-cb2a3315080b/news/800773226/
)
[id] => 800773226
[publishDate] => 2011-10-24T10:04:49
[lastModifiedDate] => 2011-10-24T11:20:40
[headline] => Relationships matter on social media
)
)
)
[format] => html
)
I want to get the value of [id] I am trying to access it like this:
echo $niList->doc->newsListItem[0]->id;
but this is giving me nothing, I know I am close (well I hope I am) but I just cant quite get it right, could anyone help please.
Thanks all.
Your object dump says
doc:XMLHandler:private
which means doc is a private property of XMLHandler. As such, you can only access it from within that object via $this. But you are trying to access it from outside the object when you do
echo $niList->newsListItem[0]->id;
This wont work. Add a method to that XMLHandler object that does what you want to do with that newslistitem id. Also see the chapter on Visibility in the PHP Manual:
http://docs.php.net/manual/en/language.oop5.visibility.php

Categories