Welcome Guest, Not a member yet? Register   Sign In
URGENT parsing xml file using codeigniter?
#1

[eluser]harper23[/eluser]
Hello all of you

Searching over internet I found http://blog.insicdesigns.com/2009/03/par...l-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?

my xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Comprobante >
<Emisor rfc="ISP900909Q88" nombre="Industrias del Sur Poniente, S.A. de C.V.">
<DomicilioFiscal calle="Alvaro Obregón" codigoPostal="06700" colonia="Col. Roma Norte" estado="Distrito Federal" localidad="México" municipio="Cuauhtémoc" noExterior="37" noInterior="3" pais="México"/>
<ExpedidoEn calle="Pino Suarez" codigoPostal="95460" colonia="Centro" estado="Nuevo Léon" localidad="Monterrey" municipio="Monterrey" noExterior="23" pais="México"/>
</Emisor>
<Receptor nombre="Rosa María Calderón Uriegas" rfc="CAUR390312S87">
<Domicilio calle="Topochico" codigoPostal="95465" colonia="Jardines del Valle" estado="Nuevo León" localidad="Monterrey" municipio="Monterrey" noExterior="52" pais="México"/>
</Receptor>
<Conceptos>
<Concepto cantidad="10" descripcion="Vasos decorados" importe="200" unidad="Caja" valorUnitario="20.00"/>
<Concepto cantidad="1" descripcion="Charola metálica" importe="150" unidad="pieza" valorUnitario="150.00"/>
</Conceptos>
<Impuestos>
<Traslados>
<Traslado impuesto="IVA" tasa="15.00" importe="52.50"/>
</Traslados>
</Impuestos>
</Comprobante>
#2

[eluser]pickupman[/eluser]
Here's a SimpleXML library I use to make this easier for you. Unzip it to your /application/libraries folder
Code:
//Load SimpleXML library
$this->load->library('simplexml');
$xmlData = $this->simplexml->xml_parse($xml); //where $xml is your xml to parse

var_dump($xmlData); //You should see an multi dimensional array for each node


Just loop through the appropriate arrays to access the data you need.
#3

[eluser]harper23[/eluser]
Thank you very much for your answers, so for loading the SimpleXML library is ok to put that code in welcome controller?, and for the xml is ok in the controller folder?

Bye and again thanks
#4

[eluser]pickupman[/eluser]
[quote author="harper23" date="1290568992"]Thank you very much for your answers, so for loading the SimpleXML library is ok to put that code in welcome controller?, and for the xml is ok in the controller folder?

Bye and again thanks[/quote]
Sure. I would keep the xml in another folder. Keep the folder logic of CI in tact. Perhaps (document root)/xml maybe more appropriate.

If the xml is a from another source (http or something), you could use curl to fetch the xml. I was using this method to fetch xml from the whitepages.com api.
#5

[eluser]harper23[/eluser]
sorry if I bother you, but I keep gettin the same error, please can you help me?

in this function, the line that is in black, I put all the directory where is my xml, but the error keep going :

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: myxml

Filename: controllers/welcome.php

Line Number: 45


sorry if is a stupid question


function _getXML($fname)
{

$filename = $fname.'.xml';
$xmlfile="C:\Users\beto\Documents\xml".$filename;
$xmlRaw = file_get_contents($xmlfile);

$this->load->library('simplexml');
$xmlData = $this->simplexml->xml_parse($xmlRaw);

foreach($xmlData['Emisor'] as $row)
{

$result .= '<tr>';
$result .= '<td>'.$row['id'].'</td>';
$result .= '<td>'.$row['name'].'</td>';
$result .= '<td>'.$row['category'].'</td>';
$result .= '<td>$ '.$row['price'].'</td>';
$result .= '</tr>';

}
return $result;
}
#6

[eluser]pickupman[/eluser]
Please use the code button in your editor, to format it properly. Without seeing how you are calling that method, I do notice that you may be leaving off a \\ from your $xmlfile string.
Try
Code:
$filename = $fname . '.xml';
$xmlfile  = "C:\\Users\\beto\\Documents\\xml\\" . $filename; //Adding extra slashes
$result   = '';

if(file_exists($xmlfile)){
  $xmlRaw = file_get_contents($xmlfile);

  $this->load->library('simplexml');
  $xmlData = $this->simplexml->xml_parse($xmlRaw);
  
  //foreach loop
}else{
  $result .= 'File ' . $xmlfile . ' was not found';
}
return $result;
#7

[eluser]harper23[/eluser]
hello thanks for answering, I get this message that tell that the xml was not found but then I get product ID, product name, I dont figure out how can I fix that, please help me

/* End of file welcome.php */ /* Location: ./system/application/controllers/welcome.php */ File“C:\Users\amaury\Documents\xml\myxml.xmlwas not found
PRODUCT ID PRODUCT NAME CATEGORY PRICE
#8

[eluser]pickupman[/eluser]
Move the file out of your Documents folder. php is not able to read the file that is in your user folder. Folder/permissions prevent that. Move into a folder for all users or something under your document root (c:\wamp\www) or something.

Again please use the code button to paste code into your post.
#9

[eluser]Musaddiq Khan[/eluser]
I have tried the foloowing code.
Code:
$xml = file_get_contents($xmlfile);
$this->load->library('simplexml');
$xmlData = $this->simplexml->xml_parse($xml);
But it givs the error.
Quote:Unable to load the requested class: simplexml
Is it needed to download this library or it is builtin class in codeigniter libraries.
Thanx
#10

[eluser]pickupman[/eluser]
PHP has changed quite since this thread. [url="http://www.php.net/manual/en/simplexml.examples-basic.php"]SimpleXML[/url] is part of PHP. I believe the simplexml library from the code above was just a wrapper to the actually PHP built in interface. Just read the example linked above, and just use the native PHP code instead of the library.





Theme © iAndrew 2016 - Forum software by © MyBB