Page 1 of 1

DomDocument and Invalid Character Error

Posted: Sun Mar 29, 2009 1:07 am
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;
 

Re: DomDocument and Invalid Character Error

Posted: Sun Mar 29, 2009 1:24 am
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?

Re: DomDocument and Invalid Character Error

Posted: Sun Mar 29, 2009 2:11 am
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!!

Re: DomDocument and Invalid Character Error

Posted: Sun Mar 29, 2009 1:41 pm
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);
  }
  }

Re: DomDocument and Invalid Character Error

Posted: Sun Mar 29, 2009 3:15 pm
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.