Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 546
  • Last Modified:

Problem with PHP simplexml_load_string reading values?

I'm trying to read some data from my xml string sown in code below.
However I can't seem to access the data.

<?

include 'shareitxml.php';

$xml = simplexml_load_string($xmlstr, NULL, LIBXML_NOERROR | LIBXML_NOWARNING);

echo $xml->e5Notification->OrderNotification->Purchase->PurchaseId;
echo $xml->Purchase->{'PurchaseId'};
echo $xml->PurchaseId;
echo $xml->Purchase[0]->PurchaseId;
echo $xml->Purchase[0]->PurchaseId;
echo $xml->OrderNotification[0]->Purchase[0]->PurchaseId;
echo $xml->e5Notification->OrderNotification[0]->Purchase[0]->PurchaseId;
echo $xml->e5Notification->OrderNotification->Purchase[0]->PurchaseId;

?>
Contents of shareitxml.php
 
<?php
$xmlstr = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<e5Notification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://xml.element5.com/2.0/notification.xsd">
<OrderNotification>
<Purchase>
<PurchaseId>137693001</PurchaseId>
<PurchaseDate>2007-07-24T15:24:40</PurchaseDate>
<PurchaseOrigin>Other</PurchaseOrigin>
<PaymentCompleteDate>2007-07-
24T15:33:42</PaymentCompleteDate>
<PaymentStatus>complete</PaymentStatus>
<SequentialInvoiceNo>e5-DE-2007-
00001091596</SequentialInvoiceNo>
<CustomerData>
<BillingContact>
<LastName>Stange</LastName>
<FirstName>Karsten</FirstName>
<Email>stange@element5.de</Email>
<Address>
<Street1>Vogelsanger Str. 78</Street1>
<City>Köln</City>
<PostalCode>50823</PostalCode>
<CountryId>DE</CountryId>
<Country>Germany</Country>
</Address>
</BillingContact>
<DeliveryContact>
<LastName>Stange</LastName>
<FirstName>Karsten</FirstName>
<Email>stange@element5.de</Email>
<Address>
<Street1>Vogelsanger Str. 78</Street1>
<City>Köln</City>
XML Schema 2.2 for XML Order Notifications Page 14 /24
<PostalCode>50823</PostalCode>
<CountryId>DE</CountryId>
<Country>Germany</Country>
</Address>
</DeliveryContact>
<CustomerPaymentData>
<PaymentMethod>Visa</PaymentMethod>
<Currency>EUR</Currency>
</CustomerPaymentData>
<Language>English</Language>
<SubscribeNewsletter>true</SubscribeNewsletter>
</CustomerData>
<PurchaseItem>
<RunningNo>1</RunningNo>
<ProductId>300170245</ProductId>
<ProductName>QA Product XML mails</ProductName>
<NotificationNo>6</NotificationNo>
<DeliveryType>Electronically</DeliveryType>
<Currency>EUR</Currency>
<Quantity>1</Quantity>
<ProductSinglePrice>10.00</ProductSinglePrice>
<VatPct>19.00</VatPct>
<Discount>0.00</Discount>
<ExtendedDownloadPrice>0.00</ExtendedDownloadPrice>
<ManuelOrderPrice>0.00</ManuelOrderPrice>
<ShippingPrice>0.00</ShippingPrice>
<ShippingVatPct>0.00</ShippingVatPct>
</PurchaseItem>
</Purchase>
</OrderNotification>
</e5Notification>
XML;
?>

Open in new window

0
mindwarpltd
Asked:
mindwarpltd
2 Solutions
 
Beverley PortlockCommented:
You can always pick single values using Xpath
<?php
$xmlstr = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<e5Notification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://xml.element5.com/2.0/notification.xsd">
<OrderNotification>
<Purchase>
<PurchaseId>137693001</PurchaseId>
<PurchaseDate>2007-07-24T15:24:40</PurchaseDate>
<PurchaseOrigin>Other</PurchaseOrigin>
<PaymentCompleteDate>2007-07-
24T15:33:42</PaymentCompleteDate>
<PaymentStatus>complete</PaymentStatus>
<SequentialInvoiceNo>e5-DE-2007-
00001091596</SequentialInvoiceNo>
<CustomerData>
<BillingContact>
<LastName>Stange</LastName>
<FirstName>Karsten</FirstName>
<Email>stange@element5.de</Email>
<Address>
<Street1>Vogelsanger Str. 78</Street1>
<City>Köln</City>
<PostalCode>50823</PostalCode>
<CountryId>DE</CountryId>
<Country>Germany</Country>
</Address>
</BillingContact>
<DeliveryContact>
<LastName>Stange</LastName>
<FirstName>Karsten</FirstName>
<Email>stange@element5.de</Email>
<Address>
<Street1>Vogelsanger Str. 78</Street1>
<City>Köln</City>
XML Schema 2.2 for XML Order Notifications Page 14 /24
<PostalCode>50823</PostalCode>
<CountryId>DE</CountryId>
<Country>Germany</Country>
</Address>
</DeliveryContact>
<CustomerPaymentData>
<PaymentMethod>Visa</PaymentMethod>
<Currency>EUR</Currency>
</CustomerPaymentData>
<Language>English</Language>
<SubscribeNewsletter>true</SubscribeNewsletter>
</CustomerData>
<PurchaseItem>
<RunningNo>1</RunningNo>
<ProductId>300170245</ProductId>
<ProductName>QA Product XML mails</ProductName>
<NotificationNo>6</NotificationNo>
<DeliveryType>Electronically</DeliveryType>
<Currency>EUR</Currency>
<Quantity>1</Quantity>
<ProductSinglePrice>10.00</ProductSinglePrice>
<VatPct>19.00</VatPct>
<Discount>0.00</Discount>
<ExtendedDownloadPrice>0.00</ExtendedDownloadPrice>
<ManuelOrderPrice>0.00</ManuelOrderPrice>
<ShippingPrice>0.00</ShippingPrice>
<ShippingVatPct>0.00</ShippingVatPct>
</PurchaseItem>
</Purchase>
</OrderNotification>
</e5Notification>
XML;
 
$xml = simplexml_load_string($xmlstr, NULL, LIBXML_NOERROR | LIBXML_NOWARNING);
$result = $xml->xpath('OrderNotification/Purchase/PurchaseId');
echo $result[0] . "<br/>";
 
$result = $xml->xpath('OrderNotification/Purchase/PurchaseDate');
echo $result[0] . "<br/>";
?>
 
Produces
 
137693001
2007-07-24T15:24:40

Open in new window

0
 
mindwarpltdAuthor Commented:
I've copied your code exactly into test.php and get this error.

PHP Fatal error: Call to a member function xpath() on a non-object in C:\phpdev\test.php on line 70
0
 
BrianMMCommented:
problem is probably encoding add this line before line 70 above;

$xmlstr = utf8_encode($xmlstr);
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now