Jump to content

DOM problem on PHP side


timcbest

Recommended Posts

I think my ajax is working fine but the XML produced by PHP 5.2.0 is coming back with this error:

XML Parsing Error: xml declaration not at start of external entity
Location: http://mysite/getcounty.php?state=MS

Line Number 1, Column 2: <?xml version="1.0" encoding="UTF-8"?>
-^

This is the function that creates the XML:
function formulateResponse($info){

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$root = $dom->createElement('response');
$root = $dom->appendChild($root);

foreach($info as $index => $county_array){
$county = $dom->createElement('county');
$county = $root->appendChild($county);

$name = $dom->createTextNode($county_array['county']);
$name = $county->appendChild($name);

}

header('Content-Type: text/xml');
echo $dom->saveXML();

}

In firebug the server response is coming back:
<?xml version="1.0" encoding="UTF-8"?>
<response>
  <county>Adams</county>
  <county>Alcorn</county>
  <county>Amite</county>
  <county>Attala</county>
  <county>Benton</county>
  <county>Bolivar</county>
  <county>Calhoun</county>
  <county>Carroll</county>
  <county>Chickasaw</county>
  <county>Choctaw</county>
  <county>Claiborne</county>
  <county>Clarke</county>
  <county>Clay</county>
  <county>Coahoma</county>
  <county>Copiah</county>
  <county>Covington</county>
  <county>De Soto</county>
  <county>Forrest</county>
  <county>Franklin</county>
  <county>George</county>
  <county>Greene</county>
  <county>Grenada</county>
  <county>Hancock</county>
  <county>Harrison</county>
  <county>Hinds</county>
  <county>Holmes</county>
  <county>Humphreys</county>
  <county>Issaquena</county>
  <county>Itawamba</county>
  <county>Jackson</county>
  <county>Jasper</county>
  <county>Jefferson</county>
  <county>Jefferson Davis</county>
  <county>Jones</county>
  <county>Kemper</county>
  <county>Lafayette</county>
  <county>Lamar</county>
  <county>Lauderdale</county>
  <county>Lawrence</county>
  <county>Leake</county>
  <county>Lee</county>
  <county>Leflore</county>
  <county>Lincoln</county>
  <county>Lowndes</county>
  <county>Madison</county>
  <county>Marion</county>
  <county>Marshall</county>
  <county>Monroe</county>
  <county>Montgomery</county>
  <county>Neshoba</county>
  <county>Newton</county>
  <county>Noxubee</county>
  <county>Oktibbeha</county>
  <county>Panola</county>
  <county>Pearl River</county>
  <county>Perry</county>
  <county>Pike</county>
  <county>Pontotoc</county>
  <county>Prentiss</county>
  <county>Quitman</county>
  <county>Rankin</county>
  <county>Scott</county>
  <county>Sharkey</county>
  <county>Simpson</county>
  <county>Smith</county>
  <county>Stone</county>
  <county>Sunflower</county>
  <county>Tallahatchie</county>
  <county>Tate</county>
  <county>Tippah</county>
  <county>Tishomingo</county>
  <county>Tunica</county>
  <county>Union</county>
  <county>Walthall</county>
  <county>Warren</county>
  <county>Washington</county>
  <county>Wayne</county>
  <county>Webster</county>
  <county>Wilkinson</county>
  <county>Winston</county>
  <county>Yalobusha</county>
  <county>Yazoo</county>
  <county>Statewide</county>
</response>

Which is correct.  How do I eliminate the '-^'? Is that an extra line?  THis started happening after I upgraded from 5.1.3 to 5.2.0. 

Thanks!
Tim
Link to comment
https://forums.phpfreaks.com/topic/31393-dom-problem-on-php-side/
Share on other sites

There is a space before your XML declaration.

Not sure what is causing it though. I was meaning to upgrade to 5.2, so I decided to test this out.

I found no problems on 5.1.4 nor 5.2..
[code]
<?php
error_reporting(E_ALL | E_STRICT);

function formulateResponse($info){
 
  $dom = new DOMDocument('1.0', 'UTF-8');
  $dom->formatOutput = true;
  $root = $dom->createElement('response');
  $root = $dom->appendChild($root); 
 
  foreach($info as $index => $county_array){
      $county = $dom->createElement('county');
      $county = $root->appendChild($county); 
     
      $name = $dom->createTextNode($county_array['county']);
      $name = $county->appendChild($name); 
 
  }
 
  header('Content-Type: text/xml');
  echo $dom->saveXML();
 
}
formulateResponse(array(array('county'=>'Adams')));
?>[/code]

[code]<?xml version="1.0" encoding="UTF-8"?>
<response>
  <county>Adams</county>
</response>[/code]

I can even get it to validate:

[code]
<?php
error_reporting(E_ALL | E_STRICT);

function formulateResponse($info){

$domImpl = new DomImplementation;
$docType = $domImpl->createDocumentType('response','','response.dtd');
$dom = $domImpl->createDocument('', 'response', $docType);
$dom->encoding = 'UTF-8';
$dom->formatOutput = true;

foreach($info as $index => $county_array){
$county = $dom->createElement('county');
$county = $dom->documentElement->appendChild($county); 

$name = $dom->createTextNode($county_array['county']);
$name = $county->appendChild($name); 

}
header('Content-Type: text/xml');
$dom->validate();
echo $dom->saveXML();

}
formulateResponse(array(array('county'=>'Adams')));
?>[/code]

[i]response.dtd[/i]
[code]<!ELEMENT response (county+)>
<!ELEMENT county (#PCDATA)>[/code]

Output:
[code]<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE response SYSTEM "response.dtd">
<response>
  <county>Adams</county>
</response>[/code]

No probs whatsoever...

How about your JS?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.