Importing DOM with SimpleXML on PHP

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Importing DOM with SimpleXML on PHP

Postposted on Sat Nov 08, 2008 10: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:
Code: Select all
<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:
Code: Select all
<?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.
Asin
Gerbil Team Leader
 
Posts: 291
Joined: Tue Mar 09, 2004 9:36 pm
Location: Ontario, Canada

Re: Importing DOM with SimpleXML on PHP

Postposted on Tue Dec 02, 2008 9: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
JdL
Gerbil Elite
 
Posts: 980
Joined: Thu Aug 29, 2002 10:45 am
Location: United States of America


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest