RE: [PHP] LoadXML trouble
| From: | ked | Date: | Tue, 11 Dec 2007 03:27:52 +0000 |
| Subject: | RE: [PHP] LoadXML trouble | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-265654@lists.php.net to get a copy of this message | ||
first, create "dealxml.php" with ansi charset :
<?php
$r =
<<<XML
<?xml version="1.0"?>
<response>
<customerID>ked</customerID>
<appName>tickcenter</appName>
<ticketType>train</ticketType>
<ticketAction>back</ticketAction>
<ticketID>1197026188_ec76</ticketID>
<status>KO</status>
<errCode>500</errCode>
<errMsg>Internal Server Error</errMsg>
</response>
XML;
$xml = new DOMDocument();
//$xml->validateOnParse = true;
$xml->loadXML( $r );
$customer_id = $xml->getElementsByTagName( 'customerID' )->item( 0
)->nodeValue;
$app_name = $xml->getElementsByTagName( 'appName' )->item( 0
)->nodeValue;
$ticket_type = $xml->getElementsByTagName( 'ticketType' )->item( 0
)->nodeValue;
$ticket_action = $xml->getElementsByTagName( 'ticketAction' )->item( 0
)->nodeValue;
$ticket_params = $xml->getElementsByTagName( 'parameters' )->item( 0
)->childNodes;
echo "$customer_id <br>";
echo "$app_name <br>";
echo "$ticket_typed <br>";
echo "$ticket_action <br>";
echo "$ticket_params <br>";
?>
IE output :
Warning: DOMDocument::loadXML() [function.loadXML]: Input is not proper
UTF-8, indicate encoding ! in Entity, line: 4 in W:\www\test\dealxml.php on
line 26
next , saved the dealxml.php as UTF-8 charset, then it works well.
I think that the xml resource should be encoded by utf-8 charset, right or
wrong ?
My OS : win2000 , simplified chinese. Dose the OS blight "DOMDocument" ?
> -----Original Message-----
> From: Nathan Nobbe [mailto:quickshiftin@gmail.com]
> Sent: Tuesday, December 11, 2007 1:25 AM
> To: Dani Castaños
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] LoadXML trouble
>
> On Dec 10, 2007 12:08 PM, Dani Castaños
> <danitao.mailists@gmail.com> wrote:
>
> > Yep, it works when i do saveXML, but not on loadXML step...
>
>
> did the loadXML() method work in the test script i sent over
> in my last post?
> if it does then something else is going on when loadXML() is
> called in the context of your application.
>
> The thing is... i'm trying to do something like this:
> >
> > $request = $_POST['xml'];
> > $logger->debug( 'New ticket request' );
> >
> > /**
> > * Parse XML request to obtain values
> > */
> > $xml = new DOMDocument();
> > //$xml->validateOnParse = true;
> > $xml->loadXML( $request );
>
>
> you should always sanitize input. at the very least you
> should be running $_POST['xml'] through a call to trim()
> before handing it to the DOMDocument instance.
>
> $request = trim($_POST['xml']);
> $xml = new DOMDocument();
> $xml->loadXML($request);
>
> i suspect there is some garbage in or around the string
> contained in $_POST['xml'] that the DomDocument instance
> doesnt like, therefore its not behaving the way youd anticipate.
> for example if you modify the script i sent over last time by
> putting some spaces in between the xml declaration and the
> opening tag of the envelope (<response>) you should see the
> following error (or something similar [depending on the value
> of error_reporting])
>
> Warning: DOMDocument::loadXML(): XML declaration allowed only
> at the start of the document in Entity,
> line: 3 in /home/nathan/testDom.php on line 19
>
> which consequently leads to
> <?xml version="1.0"?>
>
> when invoking
> echo $domDoc->saveXML() . PHP_EOL;
> rather than
> <?xml version="1.0"?>
> <response>
> <ticketID>1197026188_ec76</ticketID>
> <status>KO</status>
> <errCode>500</errCode>
> <errMsg>Internal Server Error</errMsg>
> </response>
>
> which is what you would expect.
> -nathan
>