Jump to:

23377 Posts in 18296 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » How to read xml files in Silver Stripe

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

Page: 1 2
Go to End
Author Topic: 4427 Views
  • Jardiamj
    Avatar
    Community Member
    17 Posts

    How to read xml files in Silver Stripe Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: How to read xml files in Silver Stripe Link to this post

    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.

  • Jardiamj
    Avatar
    Community Member
    17 Posts

    Re: How to read xml files in Silver Stripe Link to this post

    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!!..

  • Jardiamj
    Avatar
    Community Member
    17 Posts

    Re: How to read xml files in Silver Stripe Link to this post

    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;

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: How to read xml files in Silver Stripe Link to this post

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

  • Jardiamj
    Avatar
    Community Member
    17 Posts

    Re: How to read xml files in Silver Stripe Link to this post

    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!

  • alirobe
    Avatar
    Community Member
    35 Posts

    Re: How to read xml files in Silver Stripe Link to this post

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

  • Jardiamj
    Avatar
    Community Member
    17 Posts

    Re: How to read xml files in Silver Stripe Link to this post

    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.

    4427 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.