modify xml and display as a table [duplicate] - php

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 8 years ago.
I have this xml document.
<response>
<metadata>
<iserror>false</iserror>
<start>1</start>
<startdate>10/20/2014</startdate>
<enddate>10/21/2014</enddate>
<breakdownby>day</breakdownby>
<orderby>date</orderby>
<ordertype>asc</ordertype>
<domainname>
<![CDATA[ domainname.com ]]>
</domainname>
</metadata>
<results first="1" last="2" total="2">
<result>
<year>2014</year>
<month>10</month>
<day>03</day>
<sales>1</sales>
<profit>1.08</profit>
</result>
<result>
<year>2014</year>
<month>10</month>
<day>04</day>
<sales>4</sales>
<profit>5.79</profit>
</result>
</results>
</response>
I want to display this xml in my html page. But before that I want to make some changes to it. I want that the profit is doubled on the html page. I need help with modifying the data and then putting it as html.
any simple way to achieve this?

Look at the SimpleXML Class. This is the easiest way to go.

Related

How to get data from one tag of xml via php like script bellow? [duplicate]

This question already has answers here:
How to parse XML with namespace?
(1 answer)
Parse XML namespaces with php SimpleXML
(2 answers)
Closed 2 years ago.
so I have a XML invoice and I want to access the data in it and I am absolutely clueless on how to do it. I tried following various guides online and nothing works, I get zero errors but zero results.
How can I get data from "inv:invoiceType" tag of this xml file? Thanks!
The XML is this:
<?xml version="1.0" encoding="UTF-8"?>
<dat:dataPack id="fa001" application="StwTest" version="2.0" note="Import" xmlns:dat="http://www.stormware.cz/schema/version_2/data.xsd" xmlns:inv="http://www.stormware.cz/schema/version_2/invoice.xsd" xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd">
<dat:dataPackItem id="20007" version="2.0">
<inv:invoice version="2.0">
<inv:invoiceHeader>
<inv:invoiceType>issuedInvoice</inv:invoiceType>
<inv:number>
<typ:numberRequested>20007</typ:numberRequested>
</inv:number>
<inv:paymentType>
<typ:paymentType>draft</typ:paymentType>
</inv:paymentType>
<inv:carrier>
<typ:ids>magic horse</typ:ids>
</inv:carrier>
<inv:numberOrder>20007</inv:numberOrder>
<inv:symVar>20007</inv:symVar>
<inv:date>2020-05-11</inv:date>
<inv:dateTax>2020-05-13</inv:dateTax>
<inv:dateDue>2020-05-27</inv:dateDue>
</inv:invoiceHeader>
</inv:invoice>
</dat:dataPackItem>
</dat:dataPack>
try using SimpleXML if you are not using it.
you can follow this document : Basic SimpleXML usage

Grab value from XML Feed with PHP [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 5 years ago.
I have the following XML output from my website:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<status>0</status>
<item>
<message>OK</message>
<id>123</id>
</item>
</result>
I'd like to grab the value inside <id> & store in a variable.
Is there a way to do this without using SimpleXML?
Thank you
There are a lot of ways to do this:
See the official documentation:
XML Manipulation : http://docs.php.net/manual/en/refs.xml.php
The problem is they are all much much more complicated than SimpleXML and nowhere as fast for random access
I would go with simplexml like this:
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<result>
<status>0</status>
<item>
<message>OK</message>
<id>123</id>
</item>
</result>';
$xmlcont = new SimpleXMLElement($xml);
echo $xmlcont->item->id;
Output : 123
Refer This
Using DOM
Using simplexml_load_file

Creating an XML string from php string [duplicate]

This question already has answers here:
php parse xml string [duplicate]
(3 answers)
Closed 7 years ago.
Looking to parse some XML data that is saved in a DB. In the DB its saved in raw XML:
<?xml version="1.0" encoding="utf-8"?>
<RESPONSE>
<SUCCESS>true</SUCCESS>
<ERRORMESSAGE>
</ERRORMESSAGE>
<DATA>
</DATA>
</RESPONSE>
I want to be able to parse this data, and have found people normally using:
$xmlstr = <<<XML RAW XML XML;
But how can I use a php string in there?
So e.g.
$xmlstr = <<<XML $stringfromDB XML;
Of course that does not work, but how can it be done? Thanks.
SimpleXML should do the trick for you. Check out this link, along with php manual basics.
$xmlData = /* query xml-data from DB as string */;
$xmlObj = simplexml_load_string($xmlData);
var_dump($xmlObj);
the <<<XML ...some code... XML; is just an alternative notation for declaring (multiline) strings, you don't have to do that, as long as you can obtain full XML document from DB as in your first snippet.

XML Data Fetch help need [duplicate]

This question already has answers here:
Simple XML - Dealing With Colons In Nodes
(4 answers)
Closed 8 years ago.
I have a XML file for my project work.I need to fetch the data from the XML file.The file contains something like the following:
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:szgd="http://www.spotzot.com/gdfeed" xmlns:szbb="http://www.spotzot.com/bbfeed">
<channel>
<item>
<title>Service & Maintenance under $50</title>
<description>7 Items | Prestone, Campbell Hausfeld, Custom Accessories, Custom, KD Tools</description>
<szgd:instore>Y</szgd:instore>
</item>
</channel>
</rss>
I am using $newtitle=$newxml->channel->item->title; to fetch data from title.It successfully returned the title.
How can I fetch the data from tag in my page using php?
Use php's simplexml functions.
Load it with:
$xml = simplexml_load_string($xml); if you are having a string or
$xml = simplexml_load_file($xmlFile); if you are having a file.
if the xml was valid you will get a SimpleXmlElement. Check the following link: http://php.net/manual/en/class.simplexmlelement.php. There you will find several functions on how to get data out of it.
Btw. which data you really want to get?

Get a value from xml reponse [duplicate]

This question already has answers here:
How to get the value of an attribute from XML file in PHP?
(4 answers)
Closed 8 years ago.
I have an xml file that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<response>
<result code="1000">
<msg lang="nl">De transactie is succesvol afgerond.</msg>
</result>
<trID>
<clTRID>300100</clTRID>
<svTRID>602C9E44-3F79-564D-5A53-C9689F088A1C</svTRID>
</trID>
</response>
</epp>
I need the result code from the xml in a string.
I already tried it with simpleXml :
$resCode = new \SimpleXMLElement($info);
$text = (string)$resCode->result;`
my xml is stored in $info
But this is not working. What am I doing wrong?
Your XML path is completely off. Try this according to the xml you posted:
$text = (string)$resCode->response->result["code"];
(Hint: the first node "epp" is not needed as it is the root node. So your path starts from inside this root node.)
Some samples:
http://www.php.net/manual/en/simplexml.examples-basic.php

Categories