2012-11-27 44 views
0

我试图通过获取数据来重新构造一些XML,创建一个DOM文档,并在保存输出之前通过我需要的位进行传输,但是我不断收到“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做了一个基本错误,或是否需要找到更好的方法来解决包含标签名称“<>”的行?

我等待着你的愤怒/救赎

+0

好,**的确是*无效* XML **所以,步骤#1是用适当的实体编码来修复它('&lt;你必须......编辑&lt;'就足够了)。如果这*解析*那么,那么你就“知道”了问题和解决方案。但是,“第1行/第1列”表示''声明没有被正确读取;也许它试图把它看作一个文档片段..? – 2012-11-27 18:33:20

+0

-1因为标题可以通过“只是尝试”来回答。 – 2012-11-27 18:41:31

+0

点了,看看能否在修复之前修复xml – dex711

回答

0

你总是可以做到这一点从文档中剥离空白标签:

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

谢谢,解决了这个问题,现在它在其他一些领域抛出错误,比如“#xd;” 将开始替换这些字符串,看看我到哪里 – dex711

相关问题