Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

How to read xml files in Silver Stripe


Go to End
Reply

14 Posts   4544 Views

Avatar
Jardiamj

25 February 2009 at 9:13am Community Member, 17 Posts

Hello every one!, I started using Silver Stripe a few weeks ago and I'm liking it so far. I'm building a web site where I want to show information about different species of plants, and a bunch of .xml files with the information about the plants (Common Name, Latin Name, Habitat, etc). I know how to get the information from the files using php, but I haven't been able to do this inside Silver Stripe.
If some body can give me an example of how to do this I will appreciate it.
Thanks in advance.

Avatar
Willr

25 February 2009 at 3:33pm (Last edited: 25 February 2009 3:33pm), Forum Moderator, 5511 Posts

Where is the xml file coming from? Are you pulling it off another website or uploading it? How do you mean you have not be able to do this instead SS? All you have to do is make a function in your Page class (or Page_Controller depends on what you need to do).

Example of loading a file and outputting it

mysite/code/Page.php inside Page_Controller class

function readxml() {
$file = file_get_contents(sometextfile.xml);
$file = Convert::xml2array($file);
Debug::show($file); // $file is now just a PHP array
}

Then you can call this in your code with $this->readxml() or via URL - yoursite.com/home/readxml.

Avatar
Jardiamj

26 February 2009 at 5:11am Community Member, 17 Posts

Thanks Willr for the answer I will give it a try. I didn't write "instead SS", I wanted to write "inside of SS" but I missed the "of". I'm sorry English is not my first language. I'm uploading the files, I was stuck trying to use php simplexml to get the xml file. But I will do the way you said.
Thanks again!!..

Avatar
Jardiamj

26 February 2009 at 5:37am Community Member, 17 Posts

Hello it's me again!... Now I have other question, where should I place my xml files. First I put them in a directory in the assets directory, but I couldn't figure out how to reference them I mean I was using something like this

$file = file_get_contents("\assets\XMLFiles\ABIfir.xml") but it doesn't find the file. The debug error says "failed to open stream: No such file or directory"

I have tried placing the file everywhere in my SS directory and nothing. It could be such a dummy thing to ask but I don't know where to place the files nor how to reference them.
Thanks again;

Avatar
Willr

26 February 2009 at 9:06am Forum Moderator, 5511 Posts

try just file_get_contents("assets/XMLFiles/ABIfir.xml")

Avatar
Jardiamj

26 February 2009 at 11:06am Community Member, 17 Posts

I tried this and didn't work either!
I tried specifying the whole path like this: file_get_contents("http://localhost/SilverStripe/assets/XMLFiles/ABIfir.xml") it woks fine.
How could I make it a relative path, to my root directory I mean "http://localhost/SilverStripe".
Thanks for your help!

Avatar
alirobe

28 February 2009 at 3:45am (Last edited: 28 February 2009 3:46am), Community Member, 35 Posts

The direction of the slashes matters in UNIX based OSes - you're probably used to Windows. With most OSes you always use forward slashes.

Avatar
Jardiamj

28 February 2009 at 11:37am Community Member, 17 Posts

Hello again!!
Thanks for the advice alirobe. My problem was basically that I was trying to use a relative path instead of the complete path "http://localhost/SilverStripe/assets/XMLFiles/ABIfir.xml". It works fine now, I will make look for a way to make it a relative path, but it works fine for now.
I'm getting my xml file using PHP simplexml, then I convert it into a array and from the array into a SS DataObjetSet to use it in my template.
I'm posting my function to convert xml

Public function convertXml2Array($obj, &$arr)
{
$children = $obj->children();
foreach ($children as $elementName => $node)
{
$tag= strtolower((string)$elementName);
$text = (string)$node;
$text = trim($text);
$arr['product'][$tag] = $text;

$arr['product']['children'] = array();
$this->convertXml2Array($node, $arr['product']['children']);
}
return;
}

Then I convert the array into SS DataObjectSet that I can use in my template. It works fine.

Go to Top