Exporting defined elements from XML - php

I would like to export (or to keep) the subtags which are with the defined attribute in XML. As I don't know the name of this process, I can't find any relevant information about it on the net. And since it's hard to explain, I decided to put an examples for my issue.
Let's say, I have this XML file:
<results>
<result idSite="1">
<row>
<label>category</label>
<visits>2</visits>
<idsubdatatable>5</idsubdatatable>
<subtable>
<row>
<label>uncategorized</label>
<visits>2</visits>
<idsubdatatable>6</idsubdatatable>
<subtable>
<row>
<label>/index</label>
<visits>2</visits>
<url>http://mysite1.com/category/uncategorized/</url>
</row>
</subtable>
</row>
</subtable>
</row>
<row>
<label>about</label>
<visits>1</visits>
<idsubdatatable>7</idsubdatatable>
<subtable>
<row>
<label>/index</label>
<visits>1</visits>
<url>http://mysite1.com/about/</url>
</row>
</subtable>
</row>
</result>
<result idSite="2">
<row>
<label>/calendar</label>
<visitors>1</visitors>
<url>http://mysite2.com/calendar</url>
</row>
</result>
</results>
And I have to parse the results and keep only the rows which are with a <url> attribute. Like this:
After parsing I have to combine these rows in a new XML file, and the final result must be like this:
<result>
<row>
<label>/index</label>
<visits>2</visits>
<url>http://mysite1.com/category/uncategorized/</url>
</row>
<row>
<label>/index</label>
<visits>1</visits>
<url>http://mysite1.com/about/</url>
</row>
<row>
<label>/calendar</label>
<visitors>1</visitors>
<url>http://mysite2.com/calendar</url>
</row>
</result>
Generally I want to do this process in PHP but it maybe in other languages too.
So, if you have any idea to solve this problem, please comment.

I would use an xpath query to find all url nodes inside row nodes. Then, just append the parent node of each url element you find to a new DomDocument like so:
$xml = '...';
$dom = new DomDocument();
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml);
$new_dom = new DomDocument();
$result = $new_dom->createElement('result');
$new_dom->appendChild($result);
$xpath = new DOMXPath($dom);
$rows = $xpath->query('//row/url');
for ($i=0;$i<$rows->length;$i++) {
$node = $new_dom->importNode($rows->item($i)->parentNode, TRUE);
$result->appendChild($node);
}
$new_dom->formatOutput = TRUE;
echo $new_dom->saveXML();

I'd use simplexml to read as your input, so your parsing would be easy. And then, i'd create a recursive function such as:
function isUrlElement($element){
foreach($element->children() as $children){
if($children->getName() == 'url'){
return true;
}else{
isUrlElement($children);
}
}
}
Now this is far from complete, but you could make it recursive calling it for each children. When this returns true, you'd know you found a node that has URL as a children. Use that $element node to for example add it to an array of simplexmlelements and then just foreach it back into XML.
Does that make sense?

Related

Adding Nodes to Existing XML

The problem i was having is the Root XML was being produced every time it writes to the XML.
The Main issue was setting up Child and Defining the Root. From the help of Łza
I now understand the Root XML Node is ignored.
So then you setup and create a Child and then add your content, And example of the correct format is.
$xml = simplexml_load_file('FILENAME.xml'); // Load XML File Need to add IF Statment to create if does not exist
$result = $xml->addchild('Result'); // Ignore Root NODE and Add Child Results
$result->addChild('Time', gmdate('D-M-Y -H:i:s')); // Rest of the below adds Child to Result and outputs results
$result->addChild('Channel', $Site);
$result->addChild('Type', '**');
$result->addChild('Process', $Status);
$result->addChild('SKU', $code->SKU);
$result->addChild('item', $item);
$result->addChild('Status', '$Feedback');
$result->addChild('ErrorID', '$Error');
$result->addChild('Message', '$Message');
$xml->asXml('FILENAME.xml'); //Write to file would be
// All of the above Code is using variables from another part of the script
The output would be
<Root>
<Result>
<Time>Fri-May-2013 -09:15:22</Time>
<Channel>20</Channel>
<Type>**</Type>
<Process>Update</Process>
<SKU>98746524765</SKU>
<Item/>
<Status>Problem</Status>
<ErrorID>999-Error</ErrorID>
<Message>Unknown file format support</Message>
</Result>
<Result>
<Time>Fri-May-2013 -09:15:22</Time>
<Channel>20</Channel>
<Type>**</Type>
<Process>Update</Process>
<SKU>5412254785</SKU>
<Item/>
<Status>Problem</Status>
<ErrorID>123-Error</ErrorID>
<Message>Invalid Item</Message>
</Result>
</Root>
Thanks
Try to use SimpleXMLElement library instead hardcoded xml creation. This is maybe more complicate to use at the begining, but much more safe (I mean avoid possible errors in xml structure when you hardcode the xml) and easy to use when you just get start to use it.
And easy to add/remove nodes, childnodes.
This is an example for your code:
$xml = new SimpleXMLElement('<xml/>');
$data = $xml->addChild('data');
$result = $data->addChild('Result');
$result->addChild('Time', gmdate('D-M-Y -H:i:s'));
$result->addChild('Channel', $SiteID);
// ... and the same way create all your xml nodes.
// if you want add next <result> node witch all elements repeat the code, (or put it in loop if you want more <result> elements):
$result = $data->addChild('Result');
$result->addChild('Time', gmdate('D-M-Y -H:i:s'));
$result->addChild('Channel', $SiteID);
// and after create all nodes save the file:
$xml->asXml('DHError.xml');
above code will create xml:
<xml>
<data>
<Result>
<Time>Fri-May-2013 -12:14:39</Time>
<Channel>data</Channel>
</Result>
<Result>
<Time>Fri-May-2013 -12:14:39</Time>
<Channel>data</Channel>
</Result>
</data>
</xml>
Thats it. Then if you need to load and process the xml it would be easy:
To load the File simply use:
$xml2 = simplexml_load_file('DHError.xml');
// to add new node <Result>:
$resultNext = $xml2->data->addchild('Result');
$resultNext->addChild('Time', gmdate('D-M-Y -H:i:s'));
$resultNext->addChild('Channel', $SiteID);
//and save file
$xml2->asXml('DHError.xml');
this create a xml:
<?xml version="1.0" ?>
<xml>
<data>
<Result>
<Time>Fri-May-2013 -12:27:24</Time>
<Channel>data</Channel>
</Result>
<Result>
<Time>Fri-May-2013 -12:27:24</Time>
<Channel>data</Channel>
</Result>
<Result>
<Time>Fri-May-2013 -12:27:24</Time>
<Channel>data</Channel>
</Result>
</data>
</xml>

Populating multiple attributes into XML using PHP and MYSQL

I want to save my records from mysql table using PHP to an XML file.I am successfully retrieving records and saving into the database.
I want to populate my xml with ALL THE records in my customer table.(CustomerAccountNumber,CustomerAccountName ,AccountBalance, InvAddressLine1)
Here is my code:
<?
$newAcct=$db_accnt;
$newAcctname=$db_accntname;
$newAcctbalance=$db_accntbalance;
$xmldoc=new DOMDocument();
$xmldoc->load('XML/customer.xml');
$newElement = $xmldoc->createElement('Row');
$newAttribute = $xmldoc->createAttribute('CustomerAccountNumber');
$newAttribute->value = $newAct;
$newElement->appendChild($newAttribute);
$root->appendChild($newElement);
?>
My Question is :
How can I generate customer.xml and how can i save thousand of records in efficient way so that based on the database in THIS FORMAT :
<?xml version="1.0" standalone="yes"?>
<Rows>
<Row CustomerAccountNumber="CU002" CustomerAccountName="Customer 1" AccountBalance="289.00" />
<Row CustomerAccountNumber="5U002" CustomerAccountName="Customer 2" AccountBalance="1899.00" />
<Row CustomerAccountNumber="CU004" CustomerAccountName="Customer 3" AccountBalance="289.00" />
<Row CustomerAccountNumber="5U032" CustomerAccountName="Customer 4" AccountBalance="1899.00" />
</Rows>
I am not getting the clue what I need to do to generate multiple attributes for each record.Kindly help me
You can use XMLWriter
$xml =new XMLWriter();
$xml->openURI('file.xml');
$xml->setIndent(true);
$xml->startDocument('1.0', 'UTF-8', 'yes');
$xml->startElement('Rows');
while ( // fetch from DB ) {
$xml->startElement('Row');
$xml->writeattribute("CustomerAccountNumber", "1");
$xml->writeattribute("CustomerAccountName", "2");
$xml->writeattribute("AccountBalance", "3");
$xml->endElement();
}
$xml->endElement();
$xml->flush();
even if it's a XML, it's still a text file, you can just write it with file_put_contents().
$xml='<?xml version="1.0" standalone="yes"?>
<Rows>';
while($row=$result->mysqli_fetch_array()){
$xml.='<Row CustomerAccountNumber="'.$row[0].'" CustomerAccountName="'.$row[1].'" AccountBalance="'.$row[2].'" />';
}
$xml.='</Rows>';
file_put_contents("customer.xml", $xml);
You might want to consider using SimpleXML, which will help you in you .xml generation. It will be as simple as :
$row->addAttribute("CustomerAccountName", "name");
Documentation here : http://www.php.net/manual/en/simplexml.examples-basic.php

Unable to parse all XML tags in PHP

I am new to XML and PHP, and I am not able to parse all the tags. I tried looking for a solution and not able to find one.
The XML file is -
<?xml version="1.0" encoding="UTF-8"?>
<widgetlist>
<widget id='1'>
<content page='page1'>
<row type='header'>
<col>
<h1>Avinash</h1>
<h3>Second element</h3>
</col>
</row>
<row type='content'>
<col>
<p>Is a php programmer</p>
</col>
</row>
<row type='content'>
<col>
<h2>Heading 2</h2>
</col>
</row>
<row type='footer'>
<col>
<small>Thank You</small>
</col>
</row>
</content>
</widget>
The PHP file is -
<?php
$xml = simplexml_load_file("product.xml");
$products = $xml->xpath("//*[#type]");
var_dump($products);
for($i=0;$i<count($products);$i++)
{
print_r($i);
$count = count($products[$i]->col);
print_r(" ".$count);
print_r("\n");
$col = (array)$products[$i]->col;
$tag = key((array)$products[$i]->col);
echo "<".$tag.">".$products[$i]->col->$tag."</".$tag.">";
}
?>
Any kind of help or guidance will really be helpful. Thank you. And also, please help me out with the best practices in xml if possible, so that parsing would improve.
closing tag for
<widgetlist>
is missing in the xml file
You are missing the final tag, widgetlist. Also it might be a good idea to create an XSD file and get PHP to check that the XML matches.
This can be achieved like this
$request = new DOMDocument();
$request->loadXML($HTTP_RAW_POST_DATA, LIBXML_NOBLANKS);
if (!$request->schemaValidate('xml/Request.xsd'))
{
ErrorReply("InvalidRequest", "Schema validation returned false");
}

PHP & XML Read element by attribute value

$xml = '<?xml version="1.0"?>
<entries>
<response>
<category>client</category>
<action>Greeting</action>
<code>1000</code>
<msg>Your Connection with API Server is Successful</msg>
<resData>
<data name="svDate">2010-10-10 02:27:14</data>
</resData>
</response>
<response>
<category>client</category>
<action>Login</action>
<code>1000</code>
<msg>Command completed successfully</msg>
<value>L116:no value</value>
</response>
<response>
<category>domain</category>
<action>InfoDomain</action>
<code>1000</code>
<msg>Command completed successfully</msg>
<value>L125:no value</value>
<resData>
<data name="domain">google.com</data>
<data name="crDate">2004-12-16</data>
<data name="exDate">2013-12-16</data>
</resData>
</response>
</entries>';
$xml = simplexml_load_string($xml);
$domain = $xml->response[2]->resData[0]->data[0];
$crdate = $xml->response[2]->resData[0]->data[1];
$exdate = $xml->response[2]->resData[0]->data[2];
With the above code i can get the values.
But how can i get the values by attribute value?
For example i want to get the values with something like this:
$domain = $xml->response[2]->resData[0]->data["domain"];
$crdate = $xml->response[2]->resData[0]->data["crdate"];
$exdate = $xml->response[2]->resData[0]->data["exdate"];
One more question.
If i have two elements with the same name?
For example i would like to parse the dns. How could i do it?
The xml code is like this:
<?xml version="1.0"?>
<entries>
<response>
<category>client</category>
<action>Greeting</action>
<code>1000</code>
<msg>Your Connection with API Server is Successful</msg>
<resData>
<data name="svDate">2010-10-10 02:27:14</data>
</resData>
</response>
<response>
<category>client</category>
<action>Login</action>
<code>1000</code>
<msg>Command completed successfully</msg>
<value>L116:no value</value>
</response>
<response>
<category>domain</category>
<action>InfoDomain</action>
<code>1000</code>
<msg>Command completed successfully</msg>
<value>L125:no value</value>
<resData>
<data name="domain">google.com</data>
<data name="crDate">2004-12-16</data>
<data name="exDate">2013-12-16</data>
<data name="dns">ns1.google.com</data>
<data name="dns">ns2.google.com</data>
</resData>
</response>
</entries>
As you can see the ns1 and ns2 have the same name. name="dns".
How can i parse each one in a different variable?
Thank you!
With the element["attribute"] syntax, attribute is the name of an attribute on the element. It is not the value of some randomly chosen attribute belonging to an element.
The example below creates an array containing a mapping for the data elements of name attribute value to text value.
$data = array();
foreach ($xml->response[2]->resData->data as $d) {
$data[strtolower($d['name'])] = (string) $d;
}
// Now you can access the values via $data['domain'], $data['crdate'], etc.
Nick, your code expects XML structured like:
<resData>
<data domain="google.com" crdate="2004-12-16" exdate="2013-12-16" />
</resData>
Edit due to question change
In a marvelous dose of eating my own words, due to the change in the question an XPath approach would be more appropriate (don't you love OPs who do that?).
You can easily get an array of the name="dns" elements with a basic XPath expression.
$xml = simplexml_load_string($xml);
$dns = $xml->xpath('response[category="domain"]/resData/data[#name="dns"]');
You can use XPath to to do queries against your XML, e.g.
$entries = simplexml_load_string($xml);
$dataElements = $entries->xpath('/entries/response/resData/data[#name="dns"]');
foreach ($dataElements as $dataElement) {
echo $dataElement;
}
The above XPath finds all <data> elements with a name attribute of "dns" that are direct children of the given element hierarchy, e.g.
<entries>
…
<response>
…
<resData>
…
<data name="dns">
IMO this is easier and more appropriate than having the extra step of copying over the values into an array which would disconnect it from the actual DOM tree and which you would have to repeat for all the elements you want to map this way. XPath is built-in. You just query and get the result.
Because $dataElements is an array, you can also access the elements in it individually with
echo $dataElements[0]; // prints "ns1.google.com"
echo $dataElements[1]; // prints "ns2.google.com"
Note that the $dataElements array actually contains SimpleXmlElements connected to the main document. Any changes you do to them will also be reflected in the main document.

Manipulate xml file with php and dom

I have XML file as below:
<?xml version="1.0" encoding="UTF-8"?>
<root version="8.0.0.0">
<songs name="Album">
<row>
<song artist="artist_name">Track1</song>
</row>
<row>
<song artist="artist_name">Track2</song>
</row>
<row>
<song artist="artist_name">Track3</song>
</row>
<row>
<song artist="artist_name">Track4</song>
</row>
</songs>
</root>
Now i want to update this file with some more rows. How i can append data on top of the existing row elements? Also while adding new elements i want to check the tracks like - Track1, Track2 are not duplicates.
Currently i'm manipulating this xml file with php:dom, but its appending data at the bottom of the existing rows.
PHP code used to do above things is
<?php
//Creates XML string and XML document using the DOM
$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->Load('C:/wamp/www/xml/test1.xml');
$root = $dom->firstChild;
$list = $root->childNodes->item(1);
if(isset($_POST['submit'])){
$artistName = $_POST['name'];
$track = $_POST['track'];
$row = $dom->createElement('row');
$list->appendChild($row);
$song = $dom->createElement('song');
$row->appendChild($song);
$song->setAttribute('artist', $artistName);
$wcm_node = $dom->createTextNode($track);
$song->appendChild($wcm_node);
}
// Code to format XML after appending data
$outXML = $dom->saveXML(); // put string in outXML
//now create a brand new XML document
$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true; //yup, going to try to make this format again
//pass the output of the first bunch of stuff we did to the new XML document:
$xml->loadXML($outXML);
//now display the cleanly formatted output without using LIBXML_NOBLANKS (which may have undesired consequences
$xml->save('test1.xml'); // save as file
}
?>
Please let me know, how i can do it.
Thanks
That's not appending but prepending. DOM has a method for that, too:
DOMNode::insertBefore — Adds a new child before a reference node
Example (demo):
$dom = new DOMDocument;
$dom->loadXml('<rows><row xml:id="r1"/></rows>');
$dom->documentElement->insertBefore(
$dom->createElement('row', 'new row'),
$dom->getElementById('r1')
);
$dom->formatOutput = TRUE;
echo $dom->saveXml();
Output:
<?xml version="1.0"?>
<rows>
<row>new row</row>
<row xml:id="r1"/>
</rows>

Categories