user1794918
user1794918

Reputation: 1149

PHP XML to mysql database

I am stumped. Can't seem to access the XML array after using simplexmlelement. I have tried it all kind of ways and this site is full of examples or how to parse xml into a data array using the simplexmlelement function in PHP. I have even gone the PHP site to review the function and see how it works. I am a mid level PHP programmer. Most of these things I can figure out now. The first thing I did was to write a loop to print the data to the screen from the simplexmlelement, no go.

Here is my array:

object(SimpleXMLElement)[4]
public 'result' => string '1' (length=1)
public 'result-text' => string 'OK' (length=2)
public 'action-type' => string 'add-customer' (length=12)
public 'result-code' => string '100' (length=3)
public 'amount' => string '0.00' (length=4)
public 'customer-id' => string '675700633' (length=9)
public 'customer-vault-id' => string '675700633' (length=9)
public 'merchant-defined-field-1' => string 'Red' (length=3)
public 'merchant-defined-field-2' => string 'Medium' (length=6)
public 'billing' => 
object(SimpleXMLElement)[5]
  public 'billing-id' => string '2037042803' (length=10)
  public 'first-name' => string 'John' (length=4)
  public 'last-name' => string 'Smith' (length=5)
  public 'address1' => string '1234 Main St.' (length=13)
  public 'city' => string 'Beverly Hills' (length=13)
  public 'state' => string 'CA' (length=2)
  public 'postal' => string '90210' (length=5)
  public 'country' => string 'US' (length=2)
  public 'phone' => string '555-555-5555' (length=12)
  public 'email' => string '[email protected]' (length=16)
  public 'cc-number' => string '411111******1111' (length=16)
  public 'cc-exp' => string '1014' (length=4)
  public 'priority' => string '1' (length=1)
 public 'shipping' => 
 object(SimpleXMLElement)[6]
  public 'shipping-id' => string '416154095' (length=9)
  public 'priority' => string '1' (length=1)

I came at this array with everything I know how to do. I know better than to try to write full blown code. So I test out accessing the array like this.

    $xml = new SimpleXMLElement($data);
    echo $xml->result . "<br>";
    echo $xml->response['result-text'] . "<br>";
    echo $xml->action-type . "<br>";
    echo $xml->amount . "<br>";

This nets me nothing. The $data variable holds the xml.

      $xml = <<<XML
    <?xml version="1.0" encoding="UTF-8"?>
     <response>
      <result>1</result>
       <result-text>OK</result-text>
       <action-type>add-customer</action-type>
       <result-code>100</result-code>
       <amount>0.00</amount>
       <customer-id>1680673722</customer-id>
       <customer-vault-id>1680673722</customer-vault-id>
       <merchant-defined-field-1>Red</merchant-defined-field-1>
       <merchant-defined-field-2>Medium</merchant-defined-field-2>
   <billing>
      <billing-id>268327184</billing-id>
      <first-name>John</first-name>
      <last-name>Smith</last-name>
      <address1>1234 Main St.</address1>
      <city>Beverly Hills</city>
      <state>CA</state>
      <postal>90210</postal>
      <country>US</country>
      <phone>555-555-5555</phone>
      <email>[email protected]</email>
      <cc-number>411111******1111</cc-number>
      <cc-exp>1014</cc-exp>
      <priority>1</priority>
   </billing>
    <shipping>
        <shipping-id>998194109</shipping-id>
        <priority>1</priority>
    </shipping>
  </response>

  XML;

I know it is something simple that I am over looking but I have a really big headache now and just want to get this over with so I can move on. Thanks!

Upvotes: 0

Views: 60

Answers (1)

Steve
Steve

Reputation: 20469

There does not appear to be anything in particular wrong with your code, you just need to use the correct syntax to access the properties:

$obj = new SimpleXMLElement($xml);

echo sprintf("result: %s\n", $obj->result); //1
echo sprintf("result text: %s\n", $obj->{'result-text'}); //OK
echo sprintf("Billing first name: %s\n", $obj->billing->{'first-name'}); //john

Upvotes: 1

Related Questions