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.
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
- Page 11(current)
- 2
- Next 10 entries
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.
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!!..
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;
try just file_get_contents("assets/XMLFiles/ABIfir.xml")
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!
The direction of the slashes matters in UNIX based OSes - you're probably used to Windows. With most OSes you always use forward slashes.
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.
- Page 11(current)
- 2
- Next 10 entries