【发布时间】:2017-12-10 21:54:29
【问题描述】:
尝试使用php合并两个xml文件,然后保存一个新合并的xml。
http://alert.fcd.maricopa.gov/alert/Google/xml/fcdmc_alert_rain_v3.xml 和 http://alert.fcd.maricopa.gov/alert/Google/xml/fcdmc_alert_return.xml
<FCDMC>
<rpt_info created="07-06-2017 11:24"/>
<gage_rain id="770" last_rpt="2017-07-06T06:00:00" min_10="0.00" min_30="0.00" hour_1="0.00" hour_3="0.00" hour_6="0.00" day_1="0.00" day_3="0.00" day_7="0.00" cytd="1.77" rainscore="1" name="Tat Momolikot Dam [TM]" lat=" 32.65120" long="-111.92830"/>
和
<FCDMC>
<rpt_info created="07-06-2017 11:23"/>
<return_rain id="770" min10="0" min30="0" hour1="0" hour3="0" hour6="0" day1="0" day3="0" day7="0"/>
希望输出看起来像这样
<FCDMC>
<rpt_info created="07-06-2017 11:24"/>
<gage_rain id="770" last_rpt="2017-07-06T06:00:00" min_10="0.00" min_30="0.00" hour_1="0.00" hour_3="0.00" hour_6="0.00" day_1="0.00" day_3="0.00" day_7="0.00" cytd="1.77" rainscore="1" min10="0" min30="0" hour1="0" hour3="0" hour6="0" day1="0" day3="0" day7="0" name="Tat Momolikot Dam [TM]" lat=" 32.65120" long="-111.92830"/>
我正在尝试编写 PHP 脚本,但没有成功。我没有得到任何结果或错误。 XML 是空白的 http://alert.fcd.maricopa.gov/alert/Google/xml/merged.xml 和 php http://alert.fcd.maricopa.gov/alert/Google/v3/php/merge_test.php。这里是 sript。
$doc1 = new DOMDocument();
$doc1->load('http://alert.fcd.maricopa.gov/alert/Google/xml/fcdmc_alert_rain_v3.xml');
$doc2 = new DOMDocument();
$doc2->load('http://alert.fcd.maricopa.gov/alert/Google/xml/fcdmc_alert_return.xml');
// get 'res' element of document 1
$res1 = $doc1->getElementsByTagName('gage_rain')->item(0); //edited res - items
// iterate over 'item' elements of document 2
$items2 = $doc2->getElementsByTagName('return_rain');
for ($i = 0; $i < $items2->length; $i ++) {
$item2 = $items2->item($i);
// import/copy item from document 2 to document 1
$item1 = $doc1->importNode($item2, true);
// append imported item to document 1 'res' element
$res1->appendChild($item1);
}
$doc1->save('http://alert.fcd.maricopa.gov/alert/Google/xml/merged.xml'); //edited -added saving into xml file
有人知道这里有什么问题吗?我希望 php 将所需的输出保存为位置 http://alert.fcd.maricopa.gov/alert/Google/xml/merged.xml 的新合并 xml 文件。
【问题讨论】:
-
您能否具体说明错误和/或意外结果?
-
我想将两个 xml 文件合并到所需的输出
并将其保存到alert.fcd.maricopa.gov/alert/Google/xml/merged.xml 的新xml *添加到原始帖子中。 -
但是你现在得到的结果是什么?有什么吗?
-
我没有得到任何结果或错误。 XML 是空白的 alert.fcd.maricopa.gov/alert/Google/xml/merged.xml 和 php alert.fcd.maricopa.gov/alert/Google/v3/php/merge_test.php。