在php中组合XML文件
我获得了外部xml文件(通过文件交换),我能够读取并存储在数据库(php/mysql)中。 xml文件以不同的名称压缩,有时几个文件压缩在一个文件夹中 当文件夹只有一个xml文件时,我可以使用在php中组合XML文件,php,xml,merge,xml-parsing,Php,Xml,Merge,Xml Parsing,我获得了外部xml文件(通过文件交换),我能够读取并存储在数据库(php/mysql)中。 xml文件以不同的名称压缩,有时几个文件压缩在一个文件夹中 当文件夹只有一个xml文件时,我可以使用 $path = '/xmlFile'; //the xmlFile names are in this format : xmFile-00001235.xml, //xmlFile-000012390.xml, etc. only first portions are consis
$path = '/xmlFile';
//the xmlFile names are in this format : xmFile-00001235.xml,
//xmlFile-000012390.xml, etc. only first portions are consistent
foreach (glob($path.'/xmlFile-*.xml') as $filename)
{
$xml=file_get_contents($filename);
}
//store in database and unlink the xml file
这仅在文件夹有一个xml文件时有效,因为我在存储后取消链接xml文件,它取消链接所有文件,但只存储一个
最好的方法是什么;我正在考虑检查文件夹是否有超过1个xml并合并xml文件?也许一个样品溶液真的会有帮助
示例xml如下所示
xml1.xml
<sales>
<row id="000001" saleid="267158" amountSold="2000" />
<row id="000001" saleid="267159" amountSold="80.000" />
</sales>
xml2.xml
<sales>
<row id="000001" saleid="267160" amountSold="4000" />
<row id="000001" saleid="267161" amountSold="580" />
</sales>
基于中的答案:
$doc1=新的DOMDocument();
$doc1->load('1.xml');
$doc2=新的DOMDocument();
$doc2->load('2.xml');
//获取文档1的“res”元素
$res1=$doc1->getElementsByTagName('items')->item(0)//编辑的资源-项目
//迭代文档2的“item”元素
$items2=$doc2->getElementsByTagName('item');
对于($i=0;$i<$items2->length;$i++){
$item2=$items2->item($i);
//将项目从文档2导入/复制到文档1
$item1=$doc1->importNode($item2,true);
//将导入的项目追加到文档1“res”元素
$res1->appendChild($item1);
}
基于中的答案:
$doc1=新的DOMDocument();
$doc1->load('1.xml');
$doc2=新的DOMDocument();
$doc2->load('2.xml');
//获取文档1的“res”元素
$res1=$doc1->getElementsByTagName('items')->item(0)//编辑的资源-项目
//迭代文档2的“item”元素
$items2=$doc2->getElementsByTagName('item');
对于($i=0;$i<$items2->length;$i++){
$item2=$items2->item($i);
//将项目从文档2导入/复制到文档1
$item1=$doc1->importNode($item2,true);
//将导入的项目追加到文档1“res”元素
$res1->appendChild($item1);
}
合并多个文件可以执行以下操作
function mergeFile ( DOMDocument $target, $fileName ) {
$source = new DOMDocument();
$source->load($fileName);
foreach ( $source->getElementsByTagName("row") as $row ) {
$import = $target->importNode($row, true);
$target->documentElement->appendChild($import);
}
}
$target = new DOMDocument();
$target->loadXML('<?xml version="1.0" encoding="utf-8"?><sales></sales>');
mergeFile($target, "NewFile.xml");
mergeFile($target, "NewFile1.xml");
mergeFile($target, "NewFile2.xml");
$target->save("out2.xml");
函数合并文件(DOMDocument$target,$fileName){
$source=新的DOMDocument();
$source->load($fileName);
foreach($source->getElementsByTagName(“行”)作为$row){
$import=$target->importNode($row,true);
$target->documentElement->appendChild($import);
}
}
$target=新的DOMDocument();
$target->loadXML(“”);
mergeFile($target,“NewFile.xml”);
mergeFile($target,“NewFile1.xml”);
mergeFile($target,“NewFile2.xml”);
$target->save(“out2.xml”);
这使您可以继续将所有文件添加到一起,然后在最后保存它们。合并多个文件可以执行以下操作
function mergeFile ( DOMDocument $target, $fileName ) {
$source = new DOMDocument();
$source->load($fileName);
foreach ( $source->getElementsByTagName("row") as $row ) {
$import = $target->importNode($row, true);
$target->documentElement->appendChild($import);
}
}
$target = new DOMDocument();
$target->loadXML('<?xml version="1.0" encoding="utf-8"?><sales></sales>');
mergeFile($target, "NewFile.xml");
mergeFile($target, "NewFile1.xml");
mergeFile($target, "NewFile2.xml");
$target->save("out2.xml");
函数合并文件(DOMDocument$target,$fileName){
$source=新的DOMDocument();
$source->load($fileName);
foreach($source->getElementsByTagName(“行”)作为$row){
$import=$target->importNode($row,true);
$target->documentElement->appendChild($import);
}
}
$target=新的DOMDocument();
$target->loadXML(“”);
mergeFile($target,“NewFile.xml”);
mergeFile($target,“NewFile1.xml”);
mergeFile($target,“NewFile2.xml”);
$target->save(“out2.xml”);
这允许您继续将所有文件添加到一起,然后在最后保存它们