Generating XML based on condition in PHP - php

Is it possible to generate XML based on condition. I am getting the XML output from Database, but i want to display the nodes in such a manner based on the ID.
If id of the elements are same, they should come under one node.

Have you checked out PHP's SimpleXML? It enables you to perform XPath queries on XML data

you can handle an xml in many ways and yes you can group the xml items per db id.
imho your question needs more details:
how do you get the xml?
how do you query the database?

Related

Generating XML from XPath

I have an example where I need to update an XML node based on an XPath. SimpleXMLElement makes this easy enough by using an XPath to grab the node and then update its value in a pass-by-reference format.
However, this doesn't work at all if the node doesn't actually exist that needs to be updated. Are there any simple ways to automatically generate the XML that matches the XPath if it doesn't exist?
An example:
<example>
<childNode1>green</childNode1>
</example>
Given the XML, I could easily run the xpath command to get the <childNode1> by loading up the xml into a SimpleXMLElement and then running $xml->xpath("example/childNode1");. I could then set the value of the returned SimpleXMLElement to something new and then save the xml.
However, if I needed to set something like <childNode2> the $xml->xpath("example/childNode2") would return nothing and it wouldn't be possible to set the value or confirm that the XML was built.
Is iterating through the XPath and parsing its values the only way to confirm that each child node exists and then build them out as it goes or is there a better way to generate the necessary XPath?
XPath is a query language used for selecting nodes in an XML structure and optionally computing values based on the contents of the XML. It does not possess functions that enable the editing of the XML or automagic node creation.
PHP's SimpleXML and DOM both have XPath implementations and allow the creation or updating of XML structures; I assume you know about them since you talk about editing nodes (I can give examples if you don't). To perform the kind of node additions that you are proposing, you would need to ascertain whether the node existed (e.g. if the xpath query for example/node1 returned a node, but example/node2 returned nothing), create a new node, and add it to the XML tree. For long xpaths, e.g. example/apple/blueberry/canola/date/emblem/node1, you would indeed have to parse the path and check which elements did exist, and add those that did not.
XQuery, a more powerful, fully-featured XML query language of which XPath 2 and XPath are part, will have the ability to transform XML. XQuery is in active development, but unfortunately the current implementations (as of late 2014) lack the ability to update XML. Watch this space though!

putting xml elements in correct order for validation using php

I make modifications to an XML object using SimpleXML library and output it. But, order of the elements or attributes are not correct in the output and it does not validate with the schema.
Is there way to change order of the elements to be valid with xml schema ?
There's no way to re-order nodes using SimpleXML that I know of. Your best option may be to convert it to a DOMDocument object and then use the DOMNode->insertBefore() method to insert a new DOMNode before another one.
A good example of what you're looking to do may be http://us.php.net/manual/en/function.dom-import-simplexml.php.
source: Reorder XML nodes with php and simplexml

How can I search using PHP through an XML file and display the results?

I am trying to build a very simple price comparison script.
Until now, I wrote a code that gets some product xml feeds from shops and with the help of XSLT I create a single-global xml of all those input XMLs. I use the XSLT because the shops have different names for elements.
Now I want to take it one step further and I want to create a search form that will display me the products let's say I have the term "laptop".
I know how to create a form, but I need a coding guidance to understand how to make it to search in my XML file (products.xml) and display let's say the
Thank you
You might want to check out http://php.net/manual/en/class.xmlreader.php
Using that it is pretty easy to navigate through an XML file and grab all the info you need.
EDIT:
On second thought, http://php.net/manual/en/book.simplexml.php is a MUCH simpler way to achieve what you're trying to do. Hence the name, I guess ;)
You can use SimpleXML library to parse your xml file. In my opinion SimpleXML is easier to use than xmlreader. Though SimpleXML is introduced on php5.

Upload XML feed to mysql using PHP

I have an XML feed coming in:
<?xml version="1.0" encoding="UTF-8"?><product>
<name>John</name>
<contact_email>john#johnson.com</contact_email>
<contact_telephone>01234 567</contact_telephone>
<url>www.johnsone.com.com</url></product>
I need to get this loaded to MySQL using php - have seen a few examples but all of them take a file saved locally.
My feed is taken from the internet so changes all of the time. Does anybody have any suggestions where to start?
Thanks
First you'll need to define a data model. Then you'll need an xml parser to parse the feed, extract the data and populate your data model. Then you'll need to pass your model object to a DAO which writes the data to your database.
If it's taken from the internet you can just do
<?php
$feedData = file_get_contents('http://mywebsite/myfeed.xml');
?>
if you want to parse the data and store then
create a table for product
parse the xml and get the fields
insert into db or update existing data
so, what's your problem?
To be a bit more specific:
You'll obviously need to set up a table and database structure. I'm going to assume you have, or at least can figure out how to, set this up, and how to write to a database. If not, there are plenty of tutorials on that that should be plenty helpful. You'll need to use PHP's built-in MySQL library.
For parsing the XML you will probably want to use SimpleXML. It's not clear how your feed is coming in, but SimpleXML has the simplexml_load_string function that will let you pass it a string containing an XML document, however you get it, for parsing.
From there, you can just take the parsed XML and write it to your database. Any examples that use SimpleXML with a local file should be pretty easy to adapt using simplexml_load_string instead of simplexml_load_file, and doing whatever you're already (presumably) doing to get the data from this feed.

Best way to find updates in xml feed

I have an xml feed that I have to check periodically for updates. The xml consists of many elements and I'm looking to figure it out which is the best (and probably faster) way to find out which elements suffered updates from last time I've checked.
What I think of is to check first the lastBuildDate for modifications and if it differs from the previous one to start parse the xml again. This would involve keeping each element with all of its attributes in my database. But each element can have different number of attributes as well as other nested elements. So if it would be to store each element in my database what would be the best way to keep them ?
That's why I'm asking for your help :) Thank you.
Most modern databases will store your XML as a blob if you like. (You tagged PHP... MySQL? If so, use MEDIUMTEXT.) Store your XML and generate a diff when you get a new one. If you don't have an XML diff tool, canonicalize both XML listings then run a text diff.

Categories