johnsmith153 Posted May 18, 2012 Share Posted May 18, 2012 I am trying to use this: http://php.net/manual/en/function.xml-parse-into-struct.php to parse this: <?xml version="1.0" encoding="utf-8"?> <PaymentNotification xmlns="http://"> <PaymentMethod>card</PaymentMethod> <Hash>12345KKLS</Hash> <Payments> <Payment> <PaymentID>709750ba-b9b6-44c4-9812</PaymentID> <Amount>70</Amount> <Currency>USD</Currency> <Date>2011-05-11T07:41:44.957</Date> <StatusID>4</StatusID> </Payment> <Payment> <PaymentID>e0c66bae-c423-43c6-b896</PaymentID> <Amount>50</Amount> <Currency>USD</Currency> <Date>2011-05-11T07:42:13.55</Date> <StatusID>2</StatusID> </Payment> </Payments> </PaymentNotification> ...but can't work out which numbers to use. I only need to grab the payment ID where the statusID is 4 (for each one as could be 1 or more per xml message). It's very complicated http://php.net/manual/en/function.xml-parse-into-struct.php Link to comment https://forums.phpfreaks.com/topic/262746-httpphpnetmanualenfunctionxml-parse-into-structphp-seems-complicated/ Share on other sites More sharing options...
noXstyle Posted May 18, 2012 Share Posted May 18, 2012 Check out simpleXML: http://php.net/manual/en/book.simplexml.php http://www.php.net/manual/en/simplexml.examples-basic.php Makes your job so much easier. Link to comment https://forums.phpfreaks.com/topic/262746-httpphpnetmanualenfunctionxml-parse-into-structphp-seems-complicated/#findComment-1346702 Share on other sites More sharing options...
Barand Posted May 18, 2012 Share Posted May 18, 2012 simpleXML objects to the "xmlns='http//'", but if you remove that <?php $x = simplexml_load_file('payments.xml'); foreach($x->xpath('//Payment') as $p) { if ($p->StatusID == 2) { echo 'Amount: ', $p->Amount, ' ', $p->Currency, '<br />'; echo 'Date : ', $p->Date, '<br /><br /><br />'; } } ?> Results: Amount: 50 USD Date : 2011-05-11T07:42:13.55 Link to comment https://forums.phpfreaks.com/topic/262746-httpphpnetmanualenfunctionxml-parse-into-structphp-seems-complicated/#findComment-1346703 Share on other sites More sharing options...
requinix Posted May 18, 2012 Share Posted May 18, 2012 The namespace does add a bit of complexity, but it's still easier to deal with it than to use an array. $xml = new SimpleXMLElement( card 12345KKLS 709750ba-b9b6-44c4-9812 70 USD 2011-05-11T07:41:44.957 4 e0c66bae-c423-43c6-b896 50 USD 2011-05-11T07:42:13.55 2 XML , 0, false); $xml->registerXPathNamespace("ns", current($xml->getDocNamespaces())); $node = current($xml->xpath("//ns:Payment[ns:StatusID=4]")); Link to comment https://forums.phpfreaks.com/topic/262746-httpphpnetmanualenfunctionxml-parse-into-structphp-seems-complicated/#findComment-1346704 Share on other sites More sharing options...
Barand Posted May 18, 2012 Share Posted May 18, 2012 Thanks, I always have a problem with namespaces. This now works with the original xml giving same result <?php $x = simplexml_load_file('payments.xml'); $x->registerXPathNamespace("ns", current($x->getDocNamespaces())); foreach($x->xpath('//ns:Payment') as $p) { if ($p->StatusID == 2) { echo 'Amount: ', $p->Amount, ' ', $p->Currency, '<br />'; echo 'Date : ', $p->Date, '<br /><br /><br />'; } } ?> Link to comment https://forums.phpfreaks.com/topic/262746-httpphpnetmanualenfunctionxml-parse-into-structphp-seems-complicated/#findComment-1346706 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.