0

Searching the, Internet ,I found http://blog.insicdesigns.com/2009/03/parsing-xml-file-using-codeigniters-simplexml-library/, but I have a lot of questions. I am new to codeigniter, and I am trying to adapt my code to this xml. I want to display once a user logins, but the question is, how would I iterate through a series of child nodes with attributes? alt text

A little code will be really helpfull

Josh Davis
  • 28,400
  • 5
  • 52
  • 67
peggalvan
  • 87
  • 1
  • 2
  • 10
  • dont quite understand your requirements, but one way you can iterate through an XML file is using `foreach` – Ross Nov 23 '10 at 19:33
  • __Please__ don't post screenshots of your code. They get resized down to the point where the text is illegible, and we can't copy/paste from them even if they're reasonably sized. – Pops Nov 23 '10 at 19:36
  • Reasonably sized: http://i.imgur.com/OcVku.jpg – Michael Myers Nov 23 '10 at 19:37
  • sorry for than I just want to adapt the code of the page to iterate over my xml, the one that is displayed, thanks for your answers, I know that is using a foreach but hwo do I take for example comprobante?? – peggalvan Nov 23 '10 at 19:39

2 Answers2

4

The class you've linked to is for PHP 4. If you're using PHP 4, obviously you should upgrade now. PHP 5 users should use the real deal, SimpleXML. Here's an example for PHP's SimpleXML. If you have questions about SimpleXML (PHP's, not CodeIgniter's knockoff) read the manual's examples a dozen times and if it still doesn't come to you, please post a new question.

$Comprobante = simplexml_load_file('yourfile.xml');
echo $Comprobante->Emisor['nombre'];
Josh Davis
  • 28,400
  • 5
  • 52
  • 67
1

After you're done with $this->parser->parse, you want to look at the paragraph that starts "Lets add the _getXML function to our controller" to see how to iterate through the parsed XML object. As for attributes, they're stored in the "@attributes" property of the node with the attributes. Just do a search on the page for "attributes" and you'll get the very basic snippet of the library that assigns the attributes as well as comments asking your very question.

A good idea when you have a question about a blog post is always to try a search (on Windows, it's Ctrl+F) for your keywords. "attributes" shows up a number of times to answer your question.

Steven
  • 17,796
  • 13
  • 66
  • 118
  • thanks for your answer, so with that snippet of code I can iterate to alll my nodes and subnodes right? – peggalvan Nov 23 '10 at 19:46
  • Your usual XML node (I don't know the spec, actually, so this is just speaking from experience) will contain either a text element (sometimes CDATA) or other nodes. If it contains other nodes, just access them like you did from the parent node. – Steven Nov 23 '10 at 20:33