Solved

Problem with PHP simplexml_load_string reading values?

Posted on 2009-05-20
3
510 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
Comment Utility
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
Comment Utility
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
Comment Utility
problem is probably encoding add this line before line 70 above;

$xmlstr = utf8_encode($xmlstr);
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

763 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now