Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > simplexml problem
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 4th November 09:00
danitao.mailists
External User
 
Posts: 1
Default simplexml problem



Hi all!

I'm using simplexml to load an xml into an object.
The XML is this:
<?xml version="1.0"?>
<request>
<customerID>3</customerID>
<appName>agenda</appName>
<ticketType>cticket_agenda_server</ticketType>
<ticketTypeID>1</ticketTypeID>
<platformID>1</platformID>
<ticketAction>addUsersToGroup</ticketAction>
</request>

When I do this:

$file = simplexml_load_file( 'file.xml' );
$ticketType = $file->ticketType;

What i really have into $ticketType is a SimpleXMLElement... not the
value "cticket_agenda_server"... What am I doing wrong?

Because if I echo this value... Yes, I obtain the
"cticket_agenda_server"... But if I want to use like:

$ticket = new $ticketType( $param1, $param2);

It throws:
Uncaught exception 'Exception' with message
'SimpleXMLElement::__construct() expects parameter 1 to be string, array
given' in C:\Dani\htdocs\ticket_server\ticket_execute.php:13 4
Stack trace:
#0 C:\Dani\htdocs\ticket_server\ticket_execute.php(13 4):
SimpleXMLElement->__construct(Array, Object(clogger))
#1 {main}
thrown

It seems like it uses $ticketType class instead the value inside.

Than you in advance
  Reply With Quote


 


2 4th November 09:01
quickshiftin
External User
 
Posts: 1
Default simplexml problem



cast the value to a string when you pull it out if you want to use it that
way:
<?php $xml = <<<XML
<?xml version="1.0"?>
<request>
<customerID>3</customerID>
<appName>agenda</appName>
<ticketType>cticket_agenda_server</ticketType>
<ticketTypeID>1</ticketTypeID>
<platformID>1</platformID>
<ticketAction>addUsersToGroup</ticketAction>
</request>
XML;
$file = new SimpleXMLElement($xml);
$ticketTypeAsSimpleXmlElement = $file->ticketType;
$ticketTypeAsString = (string)$file->ticketType;
$ticketTypeAnalyzer = new ReflectionObject($ticketTypeAsSimpleXmlElement);

echo (string)$ticketTypeAnalyzer . PHP_EOL; var_dump($ticketTypeAsString);
?>

-nathan
  Reply With Quote
3 4th November 09:01
danitao.mailists
External User
 
Posts: 1
Default simplexml problem


Nathan Nobbe escribió:

Thanks! Problem fixed... But I think PHP must do the cast automatically.
I think it's a bug fixed in a further version than mine as I've read
  Reply With Quote
4 4th November 09:01
quickshiftin
External User
 
Posts: 1
Default simplexml problem


no problem; but just fyi, its not a bug; the behavior is as expected per the
manual:
http://us3.php.net/manual/en/ref.simplexml.php

*Example#6 Comparing Elements and Attributes with Text*

To compare an element or attribute with a string or pass it into a function
that requires a string,

you must cast it to a string using *(string)*. Otherwise, PHP treats the
element as an object.

if you use an operation that casts implicitly, such as echo, you dont have
to cast yourself;
otherwise you do

-nathan
  Reply With Quote


 


Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Php 5 forum, Apache forum, Iis forum, Functions forum, Classes forum, Librarys forum, Bugs forum, Postgres forum, Mysql forum, Paradox forum, Ms sql forum, Configurations forum, Php.ini forum, Problems forum, Scripting forum, Css forum, General forums, Off-topic talk, Links, Extra forums, Php


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666