问题描述
|
我必须使用的文件具有以下结构:
<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<FormattedReport xmlns = \'urn:crystal-reports:schemas\' xmlns:xsi = \'http://www.w3.org/2000/10/XMLSchema-instance\'>
<FormattedAreaPair Level=\"0\" Type=\"Report\">
<FormattedAreaPair Level=\"1\" Type=\"Details\">
<FormattedArea Type=\"Details\">
<FormattedSections>
<FormattedSection SectionNumber=\"0\">
<FormattedReportObjects>
FormattedReportObject xsi:type=\"CTFormattedField\" Type=\"xsd:long\" FieldName=\"{tblCon.ManifestNR}\"><ObjectName>ManifestNR1</ObjectName>
<FormattedValue>1,907</FormattedValue>
<Value>1907.00</Value>
</FormattedReportObject>
<FormattedReportObject xsi:type=\"CTFormattedField\" Type=\"xsd:timeInstant\" FieldName=\"{tblCon.ShippingDate}\"><ObjectName>ShippingDate1</ObjectName>
<FormattedValue>14/04/2011</FormattedValue>
<Value>2011-04-14T00:00:00</Value>
</FormattedReportObject>
... so on and so forth ...
</FormattedReportObjects>
</FormattedSection>
</FormattedSections>
</FormattedArea>
</FormattedAreaPair>
<FormattedReportObjects>
FormattedReportObject xsi:type=\"CTFormattedField\" Type=\"xsd:long\" FieldName=\"{tblCon.ManifestNR}\"><ObjectName>ManifestNR1</ObjectName>
<FormattedValue>1,907</FormattedValue>
<Value>1907.00</Value>
</FormattedReportObject>
<FormattedReportObject xsi:type=\"CTFormattedField\" Type=\"xsd:timeInstant\" FieldName=\"{tblCon.ShippingDate}\"><ObjectName>ShippingDate1</ObjectName>
<FormattedValue>14/04/2011</FormattedValue>
<Value>2011-04-14T00:00:00</Value>
</FormattedReportObject>
... so on and so forth ...
</FormattedReportObjects>
</FormattedSection>
</FormattedSections>
</FormattedArea>
</FormattedAreaPair>
<FormattedAreaPair Level=\"1\" Type=\"Details\">
<FormattedArea Type=\"Details\">
<FormattedSections>
<FormattedSection SectionNumber=\"0\">
<FormattedReportObjects>
FormattedReportObject xsi:type=\"CTFormattedField\" Type=\"xsd:long\" FieldName=\"{tblCon.ManifestNR}\"><ObjectName>ManifestNR1</ObjectName>
<FormattedValue>1,907</FormattedValue>
<Value>1907.00</Value>
</FormattedReportObject>
<FormattedReportObject xsi:type=\"CTFormattedField\" Type=\"xsd:timeInstant\" FieldName=\"{tblCon.ShippingDate}\"><ObjectName>ShippingDate1</ObjectName>
<FormattedValue>14/04/2011</FormattedValue>
<Value>2011-04-14T00:00:00</Value>
</FormattedReportObject>
... so on and so forth ...
</FormattedReportObjects>
</FormattedSection>
</FormattedSections>
</FormattedArea>
</FormattedAreaPair>
</FormattedAreaPair>
</FormattedReport>
因此,我想做的是调用一个PHP函数,该函数将解析XML并将其最终存储在sql DB中。
例如:
舱单编号:1903
发货日期:12/04/2011
运营商ID:TNT03
跟踪编号:234234232
...等等每条记录...
因此,我开始尝试使用DOM进行操作,然后偶然发现了simpleXML,我已经阅读了几本tut,并在此处搜索了实现,但我似乎无法访问最终节点(或任何其他节点)中的数据其他数据tbh)。 simpleXML是否与此类结构相对应?
我正在使用的最新PHP是:
<?PHP
if (file_exists(\'tracking.xml\')) {
$xml = simplexml_load_file(\'tracking.xml\');
// print_r($xml);
foreach( $xml as $FormattedReport->FormattedAreaPair->FormattedAreaPair )
{
foreach($FormattedReport as $node->FormattedArea->FormattedSections->FormattedSection->FormattedReportObjects)
echo $node->FormattedReportObject->Value;
}
} else {
exit(\'Failed to open xml\');
}
?>
我已经尝试将其还原为基本知识,但仍然没有运气。不回显结果。
谢谢您的时间!
解决了
处于类似情况下的任何人都需要注意一些方向。
忽略根节点,这就是导入XML字符串/文件时的默认$ variable
如果您有嵌套的组,请先创建一个到父级的节点,如$ xml-> FormattedAreaPair-> FormattedAreaPair作为$ parentnode
使用父节点遍历所有子节点
如果您有一个属性字段,请按以下方式访问它:(string)$ node [\'FieldName \'])
将检索到的属性与字符串进行比较,然后处理结果。
别把头发拉了。
//print_r($xml);
foreach( $xml->FormattedAreaPair->FormattedAreaPair as $parentnode ) {
foreach($parentnode->FormattedArea->FormattedSections->FormattedSection->FormattedReportObjects->FormattedReportObject as $node){
//echo \"FormattedValue: \".$node->FormattedValue.\"<br />\";
switch((string) $node[\'FieldName\']){
case \'{tblCon.ManifestNR}\':
echo \'Manifest: \'.$node->FormattedValue.\"<br />\";
break;
case \'{tblCon.ShippingDate}\':
echo \'Shipping Date: \'.$node->FormattedValue.\"<br />\";
break;
case \'{tblCon.CarrierID}\':
echo \'Carrier ID: \'.$node->FormattedValue.\"<br />\";
break;
case \'{tblCon.CustConRefTX}\':
echo \'Customer Reference: \'.$node->FormattedValue.\"<br />\";
break;
case \'{tblCon.ServiceCodeTX}\':
echo \'Service Code: \'.$node->FormattedValue.\"<br />\";
break;
case \'{tblCon.TotalWeightNR}\':
echo \'Total Weight: \'.$node->FormattedValue.\"<br />\";
break;
case \'{tblCon.ValueNR}\':
echo \'Value: \'.$node->FormattedValue.\"<br />\";
break;
case \'{tblCon.TotalVolumeNR}\':
echo \'Total Volume: \'.$node->FormattedValue.\"<br />\";
break;
case \'{tblCon.GoodsDesc}\':
echo \'Goods Description: \'.$node->FormattedValue.\"<br />\";
break;
case \'{tblConAddr.ReceiverNameTX}\':
echo \'Receiver Name: \'.$node->FormattedValue.\"<br />\";
break;
case \'{@SalesOrder}\':
echo \'Sales Order: \'.$node->FormattedValue.\"<br />\";
break;
case \'{@TrackingReference}\':
echo \'Tracking Reference: \'.$node->FormattedValue.\"<br />\";
break;
}
}
echo \"---------------------------- <br />\";
}
}
其他{
退出(\'无法打开xml \');
}
?>
解决方法
手册中的示例就足够了(尤其是示例4)。你似乎是一个足够聪明的家伙。问题是您做错了。
example.php
<?php
$xmlstr = <<<XML
<?xml version=\'1.0\' standalone=\'yes\'?>
<movies>
<movie>
<title>PHP: Behind the Parser</title>
<characters>
<character>
<name>Ms. Coder</name>
<actor>Onlivia Actora</actor>
</character>
<character>
<name>Mr. Coder</name>
<actor>El ActÓr</actor>
</character>
</characters>
<plot>
So,this language. It\'s like,a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
</plot>
<great-lines>
<line>PHP solves all my web problems</line>
</great-lines>
<rating type=\"thumbs\">7</rating>
<rating type=\"stars\">5</rating>
</movie>
</movies>
XML;
?>
例子#4
<?php
include \'example.php\';
$xml = new SimpleXMLElement($xmlstr);
/* For each <character> node,we echo a separate <name>. */
foreach ($xml->movie->characters->character as $character) {
echo $character->name,\' played by \',$character->actor,PHP_EOL;
}
?>
请注意,在使用“ 5”结构时,需要指定到特定类型节点的路径。 foreach
中的第二项只是一个(空)变量,可用于在迭代中存储当前节点。
,如何使用simplexml(XMLSchema-instance)访问诸如i:nil之类的属性:
Xml:
<item i:nil=\"true\"/>
Php:
(bool) $item->attributes(\'i\',true)->nil;
,我正在处理的文件约为1GB,因此无法一次全部加载xml文件。
这是我用来解析Crystal Reports XML的CI控制器。
<?php
class Parse_crystal_reports_xml extends CI_Controller {
function index(){
$base_path = \"/path/to/xml/\";
$xml_file = \"xml_file.xml\";
$file_header = \'<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\';
$separator = \'<FormattedAreaPair Level=\"1\" Type=\"Details\">\';
$xml_data = explode($separator,str_replace($file_header,\'\',file_get_contents($base_path.$xml_file)));
$bad_names = array(\'xsi:\',\'xsd:\');
foreach($xml_data as $block_num => $block) :
if(!$block_num) : continue; endif;
$fields = new SimpleXMLElement(str_replace($bad_names,$file_header.\"\\n\".$separator.$block));
$temp_array = array();
foreach($fields->FormattedArea->FormattedSections->FormattedSection->FormattedReportObjects->FormattedReportObject as $field_num => $field) :
// print_r($field);
$temp_array[$this->make_slug($field[\'FieldName\'])] = $this->clean_word((string)$field->FormattedValue);
endforeach;
// print_r($fields);
print_r($temp_array);
die;
endforeach;
}
function make_slug($string){
return strtolower(trim(preg_replace(\'/\\W+/\',\'_\',$string),\'_\'));
}
function clean_word($string){
return trim(preg_replace(\'/\\s+/\',\' \',$string));
}
}
?>