Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Asin
Gerbil Team Leader
Topic Author
Posts: 292
Joined: Tue Mar 09, 2004 10:36 pm
Location: Ontario, Canada
Contact:

Importing DOM with SimpleXML on PHP

Sat Nov 08, 2008 11:32 pm

I've whipped up a simple HTML form so that I can validate XML code that people can submit. The problem is that when I try to import the document object model using that string, I get the following warning:

Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: AttValue: " or ' expected in Entity, line: 2 in C:\WAMP\www\SCEV\validate_sce.php on line 17

Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: attributes construct error in Entity, line: 2 in C:\WAMP\www\SCEV\validate_sce.php on line 17


Here's some code snippets:

index.html:
<form action="validate_sce.php" method="post">
   <label for="sceText">SCE Text:&nbsp;&nbsp;</label>
   <textarea name="sceText" cols="120" rows="25"></textarea>
   <br />
   <br />
   <label for="validateButton">&nbsp;</label>
   <input name="validateButton" type="submit" value="Validate" />
</form>


validate_sce.php:
<?php

$sceText = $_POST["sceText"];
$sceText = str_replace( "\n", "", $sceText );
$sceText = str_replace( "\r", "", $sceText );
$sceText = "<root>\n" . $sceText . "\n</root>";

$dom = new DOMDocument;
$dom->loadXML( $sceText );

if ( !$dom ) {
   echo "Error while parsing the SCE Text.";
   exit;
} // end if

$s = simplexml_import_dom( $dom );

if ( $s = FALSE ) {
   echo "<h1>Something went wrong when importing the DOM.</h1><br />";
} // end if

echo "<h3>" . $s->Server["password"] . "</h3>";

?>


The warnings are about the '$dom->loadXML( $sceText );' line.

Any ideas? Users would be posting multi-line, well-formed XML code into the text area for validation.

I remember having trouble with multi-line data in text areas before, but I ignored it. Not sure if that's the problem here though.

Any help is greatly appreciated.
 
JdL
Graphmaster Gerbil
Posts: 1025
Joined: Thu Aug 29, 2002 11:45 am
Location: United States of America

Re: Importing DOM with SimpleXML on PHP

Tue Dec 02, 2008 10:15 am

Do you need to check IF $sceText has any values in it before doing the str_replace?

Seems like you should use to check:

if ( !$sceText ) {
echo "Error while parsing the SCE Text.";
exit;
}

prior to doing the str_replace()

Also, is the simplexml_import_dom a true/false function?
JdL
286DX > Pentium Pro 200 > Athlon 1000 @ 1.4 GHz > Athlon 64 2.2 GHz > Athlon XP 3200+ > Core 2 Duo 3300 @ 4.0 GHz > Core 2 Quad 6600 G0 @ 3.2 > Core i7 3770 @ stock

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On