空白标记名称是否会导致解析错误?

时间:2012-11-27 18:22:16

标签: php xml xml-parsing domdocument

我正在尝试通过获取数据,创建DOM文档以及在保存输出之前只传输我需要的位来重构某些XML,但是我不断得到“XML解析错误:找不到任何元素:Line 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时犯了一个基本错误,或者我是否需要找到一种更好的方法来处理包含标记名称“&lt;&gt;”的行?

我在等待你的愤怒/救赎

1 个答案:

答案 0 :(得分:0)

您可以随时从文档中删除空白标记:

$oldTree = file_get_contents($query);
$oldTree = str_replace(array('<>', '</>'), '', $oldTree);