【问题标题】:Are Blank Tag Names Causing A Parsing Error?空白标签名称是否会导致解析错误?
【发布时间】:2012-11-27 18:22:15
【问题描述】:

在保存输出之前,我正在尝试通过获取数据、创建 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 时是否犯了一个基本错误,或者我是否需要找到更好的方法来解决包含“”标签名称的行?

我等待你的愤怒/救赎

【问题讨论】:

  • 嗯,确实是 invalid XML。 所以,第一步是用正确的实体编码来修复它(&amp;lt&gt;You must .. editing&amp;lt/&gt; 就足够了) .如果这个解析,那么,你“知道”问题和解决方案。但是,“第 1 行/第 1 列”表示 &lt;?xml&gt; 声明没有被正确读取;也许它正试图将其作为文档片段阅读......?
  • -1 因为标题可以用“只是尝试一下”来回答。
  • 点,看看我是否可以在处理之前修复xml

标签: php xml xml-parsing domdocument


【解决方案1】:

您总是可以这样做以从文档中去除空白标签:

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

【讨论】:

  • 谢谢,这解决了问题,现在它在其他一些领域抛出错误,比如“#xd;”将开始替换这些字符串,看看我去哪里
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多