How do you extract fred from "<company>fred</company>"?
Moderator: General Moderators
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
How do you extract fred from "<company>fred</company>"?
I have updated this thread as a means of doing it has been lighted but still unsure.
I've been told that a stock system we connect with will pass data via an XML document to us, and from that I can query it and update our "rcstock" field.
They pass "item-code" among various "<nodes>" I think!!
I've read various web pages about this, but it's terribly confusing. I need a simple means of reading the document, and then querying (and possibly updating a database) locally. I think it's read with plenty of -> these types of code, but really am lost.
It feels like I have asked "how do I query a simple XML", and someone has given me a Harry Potter sized book to learn, when I just need the basics. And perhaps will progress from there if needed.
I've been told that a stock system we connect with will pass data via an XML document to us, and from that I can query it and update our "rcstock" field.
They pass "item-code" among various "<nodes>" I think!!
I've read various web pages about this, but it's terribly confusing. I need a simple means of reading the document, and then querying (and possibly updating a database) locally. I think it's read with plenty of -> these types of code, but really am lost.
It feels like I have asked "how do I query a simple XML", and someone has given me a Harry Potter sized book to learn, when I just need the basics. And perhaps will progress from there if needed.
Last edited by simonmlewis on Tue Mar 05, 2013 10:57 am, edited 1 time in total.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: XML - how do you read it, and update a DB from it?
SimpleXML gives you a really easy way to look at XML. From there you have a few options, a query in a set of loops being one of the least desirable, depending on what all you can do.
What does the XML look like exactly and what data do you need from it?
What does the XML look like exactly and what data do you need from it?
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: XML - how do you read it, and update a DB from it?
Hi.
I'm told it's done through a <form> post. Which puzzles me, as that's not normally in XML - I see those as posted to a variable, that I capture $name = $_POST['name'];.
This is an example, and I'd like to capture "Item-Code", and what will be called "primarystockremaining".
I then query that itemcode against my database, and if primarystockremaining is 0, I set a field to "out of stock", otherwise it's kept as "in stock".
I'm told it's done through a <form> post. Which puzzles me, as that's not normally in XML - I see those as posted to a variable, that I capture $name = $_POST['name'];.
This is an example, and I'd like to capture "Item-Code", and what will be called "primarystockremaining".
I then query that itemcode against my database, and if primarystockremaining is 0, I set a field to "out of stock", otherwise it's kept as "in stock".
Code: Select all
<romancart-transaction-data><orderid>1934945</orderid><passthru/><paid-flag>True</paid-flag><sales-record-fields><company-name/><title>Mr</title><first-name>John</first-name><last-name>Doe</last-name><address1>10 The Road</address1><address2/><town>AnyTown</town><county>AnyCounty</county><postcode>AN11TC</postcode><country>UNITED KINGDOM</country><state/><delivery-company-name/><delivery-title/><delivery-first-name/><delivery-last-name/><delivery-address1/><delivery-address2/><delivery-town/><delivery-county/><delivery-postcode/><delivery-country/><delivery-state/><delivery-phone/><phone>08702467619</phone><email>support@romancart.com</email><sub-total>4.99</sub-total><actual-discount>0</actual-discount><total-price>4.99</total-price><price-includes-tax>True</price-includes-tax><total-tax>0.74</total-tax><discount-amount>0</discount-amount><total-shipping>0</total-shipping><actual-shipping-type>FREE</actual-shipping-type><created-date>10/1/2007 3:41:31 PM</created-date><status>Charged</status><psp>RomanPay</psp><customer-email-sent>True</customer-email-sent><store-email-sent>True</store-email-sent><currency-code>GBP</currency-code><accepted-ts-and-cs>True</accepted-ts-and-cs><sourced>Unknown</sourced><shipping-status-date>10/1/2007 3:41:33 PM</shipping-status-date><shipping-status>Not Sent</shipping-status><status-date>10/1/2007 3:41:41 PM</status-date><accepts-emails>True</accepts-emails><promotional-code>ABC123</promotional-code></sales-record-fields><extra-checkout-fields><accepts-emails>True</accepts-emails><extra-checkout-field><extra-checkout-field-name>this is a test</extra-checkout-field-name><extra-checkout-field-data>test</extra-checkout-field-data></extra-checkout-field></extra-checkout-fields><order-items><order-item><item-code>Product2</item-code><item-name>Bottle of Piesporter</item-name><price>4.99</price><quantity>1</quantity></order-item></order-items></romancart-transaction-data>Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: XML - how do you read it, and update a DB from it?
Got a bit more info on this now, which may any those who want to help me, understand it more.
It's being posted in a <form>
So how do I access that as a POSTED element in my handler.php page? I guess it's to do wtih capturing the XML contents of "romancartxml", but how?
And how do you then see, for example, "postcode", and assign that to a variable.
Once I can see how to do that, I'm sorted I think.
Simon
It's being posted in a <form>
Code: Select all
<form action=romancarthandler.php method=post>
<input type=hidden name=romancartxml value=’
<romancart-transaction-data>
<orderid>1934945</orderid>
<passthru/>
<paid-flag>True</paid-flag>
<sales-record-fields>
<company-name/>
<title>Mr</title>
<first-name>John</first-name>
<last-name>Doe</last-name>
<address1>10 The Road</address1>
<address2/>
<town>AnyTown</town>
<county>AnyCounty</county>
<postcode>AN11TC</postcode>
<country>UNITED KINGDOM</country>
<state/>
<delivery-company-name/>
<delivery-title/>
<delivery-first-name/>
<delivery-last-name/>
<delivery-address1/>
<delivery-address2/>
<delivery-town/>
<delivery-county/>
<delivery-postcode/>
<delivery-country/>
<delivery-state/>
<delivery-phone/>
<phone>08702467619</phone>
<email>support@romancart.com</email>
<sub-total>4.99</sub-total>
<actual-discount>0</actual-discount>
<total-price>4.99</total-price>
<price-includes-tax>True</price-includes-tax>
<total-tax>0.74</total-tax>
<discount-amount>0</discount-amount>
<total-shipping>0</total-shipping>
<actual-shipping-type>FREE</actual-shipping-type>
<created-date>10/1/2007 3:41:31 PM</created-date>
<status>Charged</status>
<psp>RomanPay</psp>
<customer-email-sent>True</customer-email-sent>
<store-email-sent>True</store-email-sent>
<currency-code>GBP</currency-code>
<accepted-ts-and-cs>True</accepted-ts-and-cs>
<sourced>Unknown</sourced>
<shipping-status-date>10/1/2007 3:41:33 PM</shipping-status-date>
<shipping-status>Not Sent</shipping-status>
<status-date>10/1/2007 3:41:41 PM</status-date>
<accepts-emails>True</accepts-emails>
<promotional-code>ABC123</promotional-code>
</sales-record-fields>
<extra-checkout-fields>
<accepts-emails>True</accepts-emails>
<extra-checkout-field>
<extra-checkout-field-name>this is a test</extra-checkout-field-name>
<extra-checkout-field-data>test</extra-checkout-field-data>
</extra-checkout-field></extra-checkout-fields>
<order-items><order-item>
<item-code>Product2</item-code>
<item-name>Bottle of Piesporter</item-name>
<price>4.99</price>
<quantity>1</quantity>
</order-item></order-items>
</romancart-transaction-data>
’>
<input type=submit>
</form>
And how do you then see, for example, "postcode", and assign that to a variable.
Once I can see how to do that, I'm sorted I think.
Simon
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: XML - how do you read it, and update a DB from it?
Someone asked me this: "do you know how to read the data from inside tags like <postcode>TM11 1ZZ</postcode?"
The answer is no.
They asked this because they said I should forget about the XML side for a moment, and concentrate on how to read tags.
If I can suss that, I guess I'm there?!
The answer is no.
They asked this because they said I should forget about the XML side for a moment, and concentrate on how to read tags.
If I can suss that, I guess I'm there?!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do you extract fred from "<company>fred</company>"?
Why does this not work?
Posted to this...
If I echo $romancartxml2, it shows everything. But $postcode produces nothing.
Should this not work?
Code: Select all
<form action=romancarthandler.php method=post>
<input type=hidden name=romancartxml value='<title>Mr</title> <first-name>John</first-name> <last-name>Doe</last-name> <postcode>AN11TC</postcode> <price>4.99</price> <quantity>1</quantity>'>
<input type=submit>
</form>If I echo $romancartxml2, it shows everything. But $postcode produces nothing.
Code: Select all
$romancartxml = $_POST['romancartxml'];
$romancartxml2 = htmlspecialchars($romancartxml);
$count = preg_match('/<quantity>(.*)<\/quantity>/s', '$romancartxml2', $matches);
$postcode = $matches[0];
echo "$postcode";
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do you extract fred from "<company>fred</company>"?
I really dont understand what you should do. Also im really new to PHP. But i would recommend you to read SimpleXML object. There's a funciotn also that reads xml from string so u could pass the value as a variable and would create de simpleXML object. Then traverse it using its methods. XPATH is quite powerful.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do you extract fred from "<company>fred</company>"?
Hi. I've tried that. But was told to ignore the xml side for now as there should be a way to read inside particular tags.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: How do you extract fred from "<company>fred</company>"?
Try:simonmlewis wrote:Posted to this...
If I echo $romancartxml2, it shows everything. But $postcode produces nothing.Should this not work?Code: Select all
$romancartxml = $_POST['romancartxml']; $romancartxml2 = htmlspecialchars($romancartxml); $count = preg_match('/<quantity>(.*)<\/quantity>/s', '$romancartxml2', $matches); $postcode = $matches[0]; echo "$postcode";
Code: Select all
$count = preg_match('/[\<]quantity[\>](.*)[\<]\/quantity[\>]/s', '$romancartxml2', $matches);(#10850)
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do you extract fred from "<company>fred</company>"?
That echos nothing. A blank page.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do you extract fred from "<company>fred</company>"?
Code: Select all
<?php
$romancartxml = $_POST['romancartxml'];
$romancartxml2 = htmlspecialchars($romancartxml);
$count = preg_match('/[\<]quantity[\>](.*)[\<]\/quantity[\>]/s', '$romancartxml2', $matches);
$postcode = $matches[0];
echo "$postcode";
?> Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
- mecha_godzilla
- Forum Contributor
- Posts: 375
- Joined: Wed Apr 14, 2010 4:45 pm
- Location: UK
Re: How do you extract fred from "<company>fred</company>"?
Hi,
Christopher's code example does work, but you need to make sure you're not using htmlspecialchars() because your string will end up looking like this instead:
You'll need to view the page source in your browser to see that, because it looks normal when displayed in the browser.
HTH,
Mecha Godzilla
Christopher's code example does work, but you need to make sure you're not using htmlspecialchars() because your string will end up looking like this instead:
Code: Select all
<quantity>4</quantity>HTH,
Mecha Godzilla
Last edited by mecha_godzilla on Tue Mar 05, 2013 1:20 pm, edited 1 time in total.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do you extract fred from "<company>fred</company>"?
Code: Select all
<?php
$romancartxml = $_POST['romancartxml'];
$count = preg_match('/[\<]quantity[\>](.*)[\<]\/quantity[\>]/s', '$romancartxml', $matches);
$postcode = $matches[0];
echo "$postcode";
?> And if I echo $romancartxml, it just shows the text.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
- mecha_godzilla
- Forum Contributor
- Posts: 375
- Joined: Wed Apr 14, 2010 4:45 pm
- Location: UK
Re: How do you extract fred from "<company>fred</company>"?
Hi,
You need to fix this line of code:
PHP won't interpret the $romancartxml variable unless you either omit the single quotes (') or replace them with double quotes (") instead.
HTH,
M_G
You need to fix this line of code:
Code: Select all
$count = preg_match('/[\<]quantity[\>](.*)[\<]\/quantity[\>]/s', '$romancartxml', $matches);HTH,
M_G
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do you extract fred from "<company>fred</company>"?
You beauty!!
Got it. And done two as I will need to get hold of two of the parsed amounts.
Thanks SOOO much. Been working on this all day.
Code: Select all
<?php
$romancartxml = $_POST['romancartxml'];
$count = preg_match("/[\<]first-name[\>](.*)[\<]\/first-name[\>]/s", "$romancartxml", $firstname);
$count = preg_match("/[\<]postcode[\>](.*)[\<]\/postcode[\>]/s", "$romancartxml", $postcode);
$firstname = $firstname[0];
$postcode = $postcode[0];
echo "$firstname $postcode";
?> Thanks SOOO much. Been working on this all day.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.