1

我正在尝试解析一个 xml 文件并以不同的顺序打印这些项目,并在它们之间添加一些其他文本。xml文件是这样的

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

我希望输出完全像这样,两条不同的行:

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

查看引号和逗号很重要。

我正在使用它,但这还不够。我不知道接下来该怎么办...

<?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);
?>

先感谢您。

我有另一个代码

 <?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";
}
 ?>

输出是

200 Title1 something here 350 Title2 something there

我不知道如何提取数据并以我想要的方式打印它。有什么帮助吗?对不起,我是新手...

4

1 回答 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
    );
}
于 2012-12-01T01:38:47.463 回答