1

I'm trying to parse an xml file and print the items in different order and also with some other text between. The xml file is something like this

<list>
    <item>
    <price>200</price>
    <title>Title1</title>
    <description>something here</description>   
    </item>

    <item>
    <price>350</price>
    <title>Title2</title>
    <description>something there</description>  
    </item>
</list>

and I want to have the output exactly like this, 2 different lines:

"Title1","something here","","200","1",""
"Title2","something there","","350","1",""

It's important to see quotes and commas.

I'm using this, but it is not enough. I don't know what to do next...

<?php
//Initialize the XML parser
$parser=xml_parser_create();

//Function to use at the start of an element
function start($parser,$element_name,$element_attrs)
  {
  switch($element_name)
    {
    case "PRICE":
    echo "&#34;";
    break;
    case "TITLE":
    echo "&#34;";
    break;
    case "DESCRIPTION":
    echo "&#34;";
    }
  }

//Function to use at the end of an element
function stop($parser,$element_name)
  {
  echo "&#34;,";
  }

//Function to use when finding character data
function char($parser,$data)
  {
  echo $data;
  }

//Specify element handler
xml_set_element_handler($parser,"start","stop");

//Specify data handler
xml_set_character_data_handler($parser,"char");

//Open XML file
$fp=fopen("shopmania_ro.xml","r");

//Read data
while ($data=fread($fp,4096))
  {
  xml_parse($parser,$data,feof($fp)) or
  die (sprintf("XML Error: %s at line %d",
  xml_error_string(xml_get_error_code($parser)),
  xml_get_current_line_number($parser)));
  }

//Free the XML parser
xml_parser_free($parser);
?>

Thank you in advance.

I have another code

 <?php
$xfile = "file.xml";
$xparser=xml_parser_create();
xml_set_character_data_handler($xparser, "cdataHandler");

if(!($fp=fopen($xfile,"r")))
{
die ("File does not exist");
}

while($data=fread($fp, 4096))
{
if(!xml_parse($xparser,$data,feof($fp)))
{
die("XML parse error: xml_error_string(xml_get_error_code($xparser))");
}
}

xml_parser_free($xml_parser);

function cdataHandler($xparser, $cdata)
{
echo "$cdata";
}
 ?>

and the output is

200 Title1 something here 350 Title2 something there

I don't know how to extract data and print it in the way that I want. Any help? Sorry, I'm a newbie...

1
  • 1
    Checkout SimpleXML, writing an XML parser by hand is overkill for this job. Commented Dec 1, 2012 at 1:41

1 Answer 1

1

That's exactely what the XSLT stylesheet language was made for. Transforming XML documents to different formats. PHP comes with the XSL extension that's pretty easy to use. Examples are there, too.

EDIT
If that's an overkill for your purpose you should take a look at PHP's SimpleXML extension that allows you to use a node like you would use an object.

EDIT 2

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<list>
    <item>
        <price>200</price>
        <title>Title1</title>
        <description>something here</description>   
    </item>
    ...
</list>
XML;

$xml  = simplexml_load_string($xmlstr);
$list = $xml->list;

foreach ( $list->item as $item ) {
    // Not sure if this works, but if it doesn't, substitute the sprintf
    // with plain string concatenation
    echo sprintf(
        '"%s","%s","","%d","1",""',
        $item->title,
        $item->description,
        $item->price
    );
}
Sign up to request clarification or add additional context in comments.

2 Comments

Any examples would be much appreciated! Thank you.
Here you go. I just outlined the basic idea - didn't test it, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.