Jump to content

[SOLVED] XML Text Node with an Attribute


ShannyMan

Recommended Posts

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:

 

 

<Money currency="USD">40.0</Money>

Here's what my generated class looks like in my .NET simulator:

 

[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:

 

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.

 

Link to comment
https://forums.phpfreaks.com/topic/174574-solved-xml-text-node-with-an-attribute/
Share on other sites

I have an XML schema that I have to conform to.  I'm using PHP to send a message through a web service.  An example of a problematic XML node is shown here:

<Money currency="USD">40.0</Money>

 

I have no problem making a class that supports an XML attribute OR an XML element value.  However, as you see above, this node contains both an attribute AND a value.  The 'currency' attribute comes over, but I'm not sure how to get the '40.0' value in there too.

 

My PHP class is shown in my previous post.

Here's a scaled down sample of my code:

 

$strWsdlUrl = "http://localhost/WebService.asmx?WSDL";
$client = new SoapClient($strWsdlUrl); 

$MoneyTest = array('Money' => new Money('40.00', 'USD'));
$AlexMoneyTest = array('Money' => '<Money currency="USD">40.00</Money>');
$client->MoneyTest($MoneyTest);

class Money
{
public $Value;
public $currency;
public function __construct($value, $currency)
{
$this->Value = $value;
$this->currency = $currency;
}
public function toXml() 
{
  $xml = '<Money currency="' . $this->currency . '">' . $this->value . "</Money>";

  return $xml;
}
}

I don't believe this issue is with the web service. Here's the SOAP from soapUI or WSStudio(another web service tester):

 

<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:

 

<?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>

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:

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.