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
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]

Link to comment
Share on other sites

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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.