tags:

views:

185

answers:

1

Is there an easy way to do this in PHP? How do you take plain text, formatted as XML, from a form textarea and have PHP take it in as XML? I can find out how to walk the tree after it's an XML object.


Just tried richsage's technique on this sample xml:

<?xml version="1.0"?>

<videocollection>
    <title id="1">Tootsie</title>
    <genre>comedy</genre>
    <year>1982</year>
    <language>English</language>
    <cast>Dustin Hoffman</cast>
    <cast>Jessica Lang</cast>
    <cast>Teri Gar</cast>
    <cast>Sydney Pollak</cast>
    <crew>
      <director>Sydney Pollak</director>
    </crew>

    <title id="2">Jurassic Park</title>
    <genre>science fiction</genre>
    <year>1993</year>
    <language>English</language>
    <cast>Sam Neil</cast>
    <cast>Laura Dern</cast>
    <cast>Jeff Goldblum</cast>
    <crew>
      <director>Steven Spielberg</director>
    </crew>

    <title id="3">Mission Impossible</title>
    <genre>action</genre>
    <year>1996</year>
    <language>English</language>
    <cast>Tom Cruise</cast>
    <cast>Jon Voight</cast>
    <cast>Emmanuelle Beart</cast>
    <cast>Jean Reno</cast>
    <crew>
      <director>Brian de Palma</director>
    </crew>
</videocollection>

Here's my code:

<html>
<head><title></title></head>
<body>
</body>

<?php
echo $_SERVER['PHP_SELF'];
$in_xml=$_POST['in_xml'];
if ($in_xml) {
    print "We got it!...";// . $in_xml;
    $doc = new DOMDocument();
    $doc->loadXML( $in_xml);
} else {
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<textarea name="in_xml" cols="40" rows="10">
  Enter your comment here
</textarea>
<input type="submit" value="Submit">
<?php
}
?>

</html>

Got a whole bunch of warnings: Warning: DOMDocument::loadXML() [domdocument.loadxml]: String not started expecting ' or " in Entity, line: 1 in /Library/WebServer/Documents/sandbox/parse_xml/index.php on line 12

Warning: DOMDocument::loadXML() [domdocument.loadxml]: Malformed declaration expecting version in Entity, line: 1 in /Library/WebServer/Documents/sandbox/parse_xml/index.php on line 12

Warning: DOMDocument::loadXML() [domdocument.loadxml]: Blank needed here in Entity, line: 1 in /Library/WebServer/Documents/sandbox/parse_xml/index.php on line 12

Warning: DOMDocument::loadXML() [domdocument.loadxml]: parsing XML declaration: '?>' expected in Entity, line: 1 in /Library/WebServer/Documents/sandbox/parse_xml/index.php on line 12

Warning: DOMDocument::loadXML() [domdocument.loadxml]: AttValue: " or ' expected in Entity, line: 4 in /Library/WebServer/Documents/sandbox/parse_xml/index.php on line 12

Warning: DOMDocument::loadXML() [domdocument.loadxml]: attributes construct error in Entity, line: 4 in /Library/WebServer/Documents/sandbox/parse_xml/index.php on line 12

Warning: DOMDocument::loadXML() [domdocument.loadxml]: Couldn't find end of Start Tag title line 4 in Entity, line: 4 in /Library/WebServer/Documents/sandbox/parse_xml/index.php on line 12

Warning: DOMDocument::loadXML() [domdocument.loadxml]: Opening and ending tag mismatch: videocollection line 3 and title in Entity, line: 4 in /Library/WebServer/Documents/sandbox/parse_xml/index.php on line 12

Warning: DOMDocument::loadXML() [domdocument.loadxml]: Extra content at the end of the document in Entity, line: 5 in /Library/WebServer/Documents/sandbox/parse_xml/index.php on line 12

+3  A: 

You can use the DOMDocument class in PHP:

$doc = new DOMDocument();
$doc->loadXML($yourXML);

where $yourXML is the XML data you've obtained from your form (eg through $_POST or similar). Once you've got this in, you can walk the object as you described, or use XPath or similar to navigate and select elements as you wish.

richsage
Thanks richsage...this is exactly the functionality that I want. I tried it using sample xml but got lots of errors when I input via the textbox. If I hardcode the text into my php, things seem to be fine. Maybe I need to escape the input from the textarea?
milesmeow
You might have magic quotes on or similar (see http://php.net/manual/en/security.magicquotes.php). Try disabling this and seeing if it's any better?
richsage
PS what errors were you seeing?
richsage
I've updated my post...please see above. I've also included the code, some sample xml (which is not the real xml) and the warnings I get.
milesmeow
I've used your code and sample XML exactly, and I get no errors. Can you post some *actual* input that's being submitted?
richsage