Solved

Problem with PHP simplexml_load_string reading values?

Posted on 2009-05-20
3
516 Views
Last Modified: 2013-12-13
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
Comment
Question by:mindwarpltd
3 Comments
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 100 total points
ID: 24431413
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
 

Author Comment

by:mindwarpltd
ID: 24431540
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
 
LVL 11

Accepted Solution

by:
BrianMM earned 400 total points
ID: 24431744
problem is probably encoding add this line before line 70 above;

$xmlstr = utf8_encode($xmlstr);
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jQuery Toggle & Anchor Links 5 44
PHP and MSSQL Arrays and Variables 3 26
xslt 1.0 - How to split value 8 28
show child records separated by commas 12 12
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question