DomDocument and Invalid Character Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
archyve
Forum Newbie
Posts: 3
Joined: Sun Mar 29, 2009 1:02 am

DomDocument and Invalid Character Error

Post by archyve »

Below is the code that im trying to get to work.. it works fine except when i try to enter a path using $image_path to add it before the $field_name. Then i get a "Invalid Character Error" due to the / in the path. I've been trying for a while to find answers and can't seem to find anything... unless its just a limitation of DomDocument. Any help would be appreciated!

Code: Select all

 
$query = "SELECT med_file, med_title FROM media WHERE `med_album_id` = 3 ORDER BY `med_date` DESC";
$resultID = mysql_query($query, $linkID) or die("Data not found.");
$table_id = 'test';
$image_path = "/path/";
 
$doc = new DomDocument('1.0');
 
$root = $doc->createElement('content');
$root = $doc->appendChild($root);
 
while($row = mysql_fetch_assoc($resultID)) {
 
$occ = $doc->createElement('image');
$occ = $root->appendChild($occ);
 
// add a child node for each field
foreach ($row as $fieldname => $fieldvalue) {
    $fieldname = $image_path . $fieldname;
    $child = $doc->createElement($fieldname);
    $child = $occ->appendChild($child);
    $value = $doc->createTextNode($fieldvalue);
    $value = $child->appendChild($value);   
  } // foreach
} // while
 
$xml_string = $doc->saveXML();
echo $xml_string;
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: DomDocument and Invalid Character Error

Post by requinix »

You can't have a slash in a tag name (which is what you're providing to createElement). It's a requirement of XML, not DOMDocument.

Why are you trying to accomplish by attaching the $image_path?
archyve
Forum Newbie
Posts: 3
Joined: Sun Mar 29, 2009 1:02 am

Re: DomDocument and Invalid Character Error

Post by archyve »

lol. :banghead:

Thats what i get for staring at something too long and not leaving it alone.

what i was doing was supposed to be affecting $fieldvalue not $fieldname.

fixes and works fine.

thanks for pointing out the freaking obvious!!
archyve
Forum Newbie
Posts: 3
Joined: Sun Mar 29, 2009 1:02 am

Re: DomDocument and Invalid Character Error

Post by archyve »

Got one more issue with DomDocument... and it gives me the following erro: Argument 1 passed to DOMNode::removeChild() must be an instance of DOMNode, null given on line 23.. which is:

Code: Select all

$deleted = $xml->removeChild($deleteimg);
The following loops through <images>... </images> getting me the med_id.. which is the main ID for the image.
What i want to do is when the ID equals a certain value, delete that child. I'm using $i for the row. When i run the test "Working - Deletes first row" I can see how it works, I just don't understand the DOMNodes enough

Code: Select all

 
$source = 'test6.xml';
 
$xml = new DomDocument('1.0');
$xml->load($source);
 
// Working - Deletes first row!
// $book = $xml->documentElement;
// we retrieve the chapter and remove it from the book
// $chapter = $book->getElementsByTagName('image')->item(0);
// $oldchapter = $book->removeChild($chapter);
// $url = $xml->save("test6.xml");
 
$images = $xml->getElementsByTagName("image");
$i=0;
foreach( $images as $image )
  {
  $i++;
  $med_ids = $image->getElementsByTagName("med_id");
  $med_id = $med_ids->item(0)->nodeValue;
  if ($med_id == 6) {
      $deleteimg = $xml->getElementsByTagName('image')->item($i);
      $deleted = $xml->removeChild($deleteimg);
  }
  }
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: DomDocument and Invalid Character Error

Post by requinix »

It can delete all but one of the <image> elements. "All but one?" Yep.

Line 14: say that gives 5 <image> tags, and say they all are to be deleted. $i starts at 0, then gets incremented to 1 inside the loop.
On line 22 it gets the same list of <image>s but selects the second one (remember, starts counting at zero).
Next pass through the loop, $i=2 and $deleteimg is the third one.
Then $i=3 and $deleteimg is the fourth, then $i=4 and $deleteimg is the fifth.
Now when $i=5, on the last pass through the loop, $deleteimg will be the sixth one - except there are only five. So now $deleteimg is null and you can't remove it since it's not a node.

Solution: don't increment $i until the end of the loop.


Also it looks like you're using PHP 5. Have you heard about SimpleXML? It's easier to use that when dealing with XML than DOMDocument.
Post Reply