Page 1 of 1

XML Text Node with an Attribute

Posted: Thu Sep 17, 2009 7:49 am
by ShannyMan
I'm working on taking some PHP classes and serializing them into XML. Rather than using arrays, I'm just using classes with public properties named the same as the XML nodes. This is working perfectly until I get to a XML text node that has an attribute attached to it. Here's an example:

Code: Select all

<Money currency="USD">40.0</Money>
Here's what my generated class looks like in my .NET simulator:

Code: Select all

 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public partial class Money
{
 
    private string currencyField;
 
    private string valueField;
 
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string currency
    {
        get
        {
            return this.currencyField;
        }
        set
        {
            this.currencyField = value;
        }
    }
 
    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value
    {
        get
        {
            return this.valueField;
        }
        set
        {
            this.valueField = value;
        }
    }
}
 
Here's what my PHP class looks like:

Code: Select all

 
class Money
{
public $Value;
public $currency;
public function __construct($value, $currency)
{
$this->Value = $value;
$this->currency = $currency;
}
}
 
The value of currency comes through, but the actual value of Money does not. Anyone know how to do this? Changing XML schema's is not an option since it's set by our client.

Thanks.

Re: XML Text Node with an Attribute

Posted: Thu Sep 17, 2009 1:19 pm
by requinix
The .NET code doesn't really matter here. What does is the PHP code that gets the XML data to the Money class.

And I'm curious: why do you want/need objects for this?

Re: XML Text Node with an Attribute

Posted: Thu Sep 17, 2009 1:25 pm
by ShannyMan
You're right, the .NET code is just so I had as much info as possible. The .NET app simulates the web service endpoint, and that's what the Money class it generated looks like. Sorry if that's confusing anyone. :)

I also don't need to use objects, but I just prefer to so that I'm not using arrays of arrays of arrays, etc. In my opinion it also better represents the XML structure.

Up until I have a node with both a text value and an attribute, such as the Money node above, the classes/objects work perfectly. I'm just not sure how to represent this particular scenario with BOTH the attribute AND text value.

Re: XML Text Node with an Attribute

Posted: Thu Sep 17, 2009 2:23 pm
by requinix
Would still help to see some code.

Re: XML Text Node with an Attribute

Posted: Thu Sep 17, 2009 3:02 pm
by ShannyMan
Here is a scaled down version of my code, with only the Money node.

Code: Select all

 
    $strWsdlUrl = "http://localhost/WebService.asmx?WSDL";
    $client = new SoapClient($strWsdlUrl); 
    $MoneyTest = array('Money' => new Money('40.00', 'USD'));
    $client->MoneyTest($MoneyTest);
        
    class Money
    {
        public $Value;
        public $currency;
        public function __construct($value, $currency)
        {
            $this->Value = $value;
            $this->currency = $currency;
        }
    }
 

Re: XML Text Node with an Attribute

Posted: Fri Sep 18, 2009 9:02 am
by ShannyMan
I don't believe this issue is with the web service. Here's the SOAP from soapUI or WSStudio(another web service tester):

Code: Select all

 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.myapp.com/">
<soapenv:Header />
<soapenv:Body>
<ws:MoneyTest>
<!--Optional:-->
<ws:Money currency="USD">40.0</ws:Money>
</ws:MoneyTest>
</soapenv:Body>
</soapenv:Envelope>
 
Here's the SOAP that PHP generates:

Code: Select all

 
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://ws.myapp.com/">
<SOAP-ENV:Body>
<ns1:MoneyTest>
<ns1:Money currency="USD" />
</ns1:MoneyTest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
 

Re: XML Text Node with an Attribute

Posted: Fri Sep 18, 2009 12:41 pm
by ShannyMan
I figured it out! For any XML nodes like this, the value needs to be represented with a '_' in either the array, or as a class property. Here's my PHP class:

Code: Select all

 
    class Money
    {
        public $_;
        public $currency;
        public function __construct($value, $currency)
        {
            $this->_ = $value;
            $this->currency = $currency;
        }
    }