0

在保存输出之前,我试图通过获取数据、创建 DOM 文档并仅传输我需要的位来重组一些 XML,但是我不断收到“XML 解析错误:未找到元素:第 1 行,第 1 列:“错误。我认为这与第一个里面的空白标签有关:

<?xml version="1.0" encoding="UTF-8"?>
<report title="My Programs" name="aAffiliateMyProgramsReport" time="2012-11-27 16:06">
<matrix rowcount="2">
<rows>
 <row>
  <>You must select one or more sale events before editing</>
 </row>
</rows>
</matrix>
<matrix rowcount="2343">
    <rows>
        <row>
            <siteName>thewebsite.com</siteName>
            <affiliateId>123456</affiliateId>
            <programName>TheProgram.com</programName>
            <currentStatusExcel>Ok</currentStatusExcel>
            <programId>203866</programId>
            <applicationDate>2012-09-15</applicationDate>
            <programTariffAmount>0.0</programTariffAmount>
            <programTariffCurrency>GBP</programTariffCurrency>
            <programTariffPercentage>0.0</programTariffPercentage>
            <status>Accepted</status>
            <event>Unique visitor</event>
            <eventIdView>2</eventIdView>
            <eventLastModified>2011-03-15</eventLastModified>
            <segmentID>1</segmentID>
            <segmentName>General</segmentName>
            <lastModified>2012-09-15</lastModified>
        </row>........

这是我要运行的 PHP:

//contents of MyPrograms report - tested $query in browser many times: it is correct
$query = $q1.$siteID.$q2.$rKey.$q3;

//create DOM document for newTree
$newTree = new DOMDocument();
$newTree->formatOutput =true;
$r = $newTree->createElement ("ProgramTariffs");
$newTree->appendChild($r);

//load contents of MyPrograms report into an xml element
//$oldTree = simplexml_load_file($query);
//that wasn't working so tried file_get_contents instead
$oldTree = file_get_contents($query);

//the above is now at least allowing this script to produce an xml file, but it just contains 
"<?xml version="1.0"?> <ProgramTariffs/>" 
//and still throws the no element found error.................................

//for each instance of a program id in $oldTree.....
foreach($oldTree->matrix->rows->row as $program)
    { //an attempt to skip over first $program if nothing is set
    if (!empty($program->programId)) {

//create the top line container tag
        $row = $newTree->createElement ("programTariff");

//create the container tag for programId
        $progID = $newTree->createElement("programId");
        //fill it with the information you want
        $progID->appendChild ( $newTree->createTextNode ( $program->programId ) );
        //attach this information to the row
        $row->appendChild($progID);

//create the container tag for eventName
        $eventName = $newTree->createElement("eventName");
        //fill it with the information you want
        $eventName->appendChild ( $newTree->createTextNode ( $program->event ) );
        //attach this information to the row
        $row->appendChild($eventName);

//create the container tag for eventAmount
        $eventPercent = $newTree->createElement("eventPercent");
        //fill it with the information you want
        $eventPercent->appendChild ( $newTree->createTextNode ( $program->programTariffPercentage ) );
    //attach this information to the row
        $row->appendChild($eventPercent);

  //attach all of the above to a row in NewTree
    $r->appendChild ($row);
     }
}
//save the output
$newTree->save("ProgramTariffs.xml");

我在访问原始 XML 时是否犯了一个基本错误,或者我是否需要找到一种更好的方法来解决包含 "<>" 标记名称的行?

我等待你的愤怒/救赎

4

1 回答 1

0

您始终可以这样做以从文档中删除空白标签:

$oldTree = file_get_contents($query);
$oldTree = str_replace(array('<>', '</>'), '', $oldTree);
于 2012-11-27T18:37:10.190 回答