php xpath iterating through inner nodes, get values based on specific attributes - php

I have an XML document in following structure.
<rows pos="0" rec_count="200">
<row id="109">
<cell style="" fieldName="pid">104</cell>
<cell style="" fieldName="title">Mr.</cell>
<cell style="" fieldName="name">Vladimir</cell>
<cell style="" fieldName="projectcode">879</cell>
<cell style="" fieldName="clientstatus">1000000000</cell>
</row>
<row id="111">
<cell style="" fieldName="pid">105</cell>
<cell style="" fieldName="title">Mr.</cell>
<cell style="" fieldName="name">Barak</cell>
<cell style="" fieldName="projectcode">789/cell>
<cell style="" fieldName="clientstatus">1000000000</cell>
</row>
</rows>
Now I need to get value of each row, to an array, which each row element is an associative array where fieldName of above becomes key, element value is node value.
How can I do accomplish this with XPath?
I tried several methods; none worked for me yet.
Thanks #JeffreyBosboom , #Makyen for your reply.
The most successfull I came is as follows.
foreach ($xml->xpath('//row') as $node) {
foreach($node->cell as $cell) {
var_dump((string)$cell[0]);
}
}
With it I can get the node value. But I need to get the fieldName attribute extracted in each iteration

Do you mean something like this:
$arr = array();
$s = simplexml_load_string($xml);
//First we search all the <cell> nodes within <row> nodes
foreach($s->xpath("//row/cell") as $node)
{
//For all found nodes we retrieve its ID from ancestor (<row>)
//attribute named id
$id = $node->xpath("../#id");
$id = (int)$id[0];
if(!isset($arr[$id]))
{
$arr[$id] = array();
}
//Then we iterate through all attributes of found <cell> nodes
//to find the attribute named fieldName
foreach($node->attributes() as $key=>$att)
{
if($key=='fieldName')
{
$arr[$id][(string)$att] = (string)$node;
}
}
}
//In $arr array there will be desired output
echo "<pre>";
print_r($arr);
echo "</pre>";
?

Related

How to break 'foreach' in php when reach a specific tag name?

I'm trying to import a .xml table file to mysql using php, it worked fine, but i want to upgrade my code so i can recognize more variations of xml tables.
So basically the problem is, i got this code ( just a example, my real table is bigger) of .xml file that I'm trying to read:
...
<Table ss:StyleID="s62">
<Column ss:StyleID="s62"/>
<Column ss:StyleID="s62"/>
<Column ss:StyleID="s62"/>
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s75"><Data ss:Type="String">Mercado</Data></Cell>
<Cell ss:StyleID="s75"><Data ss:Type="String">Segmento</Data></Cell>
<Cell ss:StyleID="s76"><Data ss:Type="String">Codigo do Projeto</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s90"><Data ss:Type="String">Mineração</Data></Cell>
<Cell ss:StyleID="s90"><Data ss:Type="String">Portuário</Data></Cell>
<Cell ss:StyleID="s90"/>
</Row>
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s90"><Data ss:Type="String">Portuário</Data></Cell>
<Cell ss:StyleID="s90"/>
<Cell ss:StyleID="s90"><Data ss:Type="String">Greenfield</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s90"/>
<Cell ss:StyleID="s90"><Data ss:Type="String">Greenfield</Data></Cell>
<Cell ss:StyleID="s90"><Data ss:Type="String">Large CapEx>>maior que 500MBRL</Data></Cell>
</Row>
</Table>
<Worksheet ss:Name="cod">
<Table ss:StyleID="s62">
... ...
</Table>
...
Well, what i want to do is to get the row and data element using getElementByTagName, but i just want to get whats inside the first Table element, not the second, third and so on...
This is what I've tried:
$tabelas = $arquivo->getElementsByTagName("Table");
$rows = $arquivo->getElementsByTagName("Row");
$contRow = 1; (This is just to create a condition to jump the first row)
$contTabelas = TRUE;
foreach ($tabelas as $tabela) {
if ($contTabelas) {
foreach ($rows as $row) {
if ($contRow > 1) {
$Mercado = $row->getElementsByTagName("Data")->item(0)->nodeValue;
$Segmento = $row->getElementsByTagName("Data")->item(1)->nodeValue;
$CodigoDoProjeto = $row->getElementsByTagName("Data")->item(2)->nodeValue;
}
$contRow++;
}
$contTabelas = FALSE;
}
}
It seems that the "foreach($rows as $row)" is getting all the rows from the xml file, but i just want whats inside the "Table" tag. How can I do that??
P.S: I got another problem to solve later, there are a lot of row without an item (Data tag) inside, so i cant get those and the program just jumps to the next one, but i think the solution is just get the 'cell' tag instead 'data'.
That looks like an OpenXML spreadsheet if that is the case you should look for the namespace definitions. I expect you to find xmlns="urn:schemas-microsoft-com:office:spreadsheet" and xmlns::ss="urn:schemas-microsoft-com:office:spreadsheet".
This is the same namespace actually, but XML attributes do not have a default namespace, so they need an prefix/alias.
With that you can use Xpath expressions to fetch specific data from the document:
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
$xpath->registerNamespace('spreadsheet', 'urn:schemas-microsoft-com:office:spreadsheet');
$records = [];
$rows = $xpath->evaluate('((//spreadsheet:Table)[1]/spreadsheet:Row)[position() > 1]');
foreach ($rows as $row) {
$records[] = [
'Mercado' => $xpath->evaluate('string(spreadsheet:Cell[1])', $row),
'Segmento' => $xpath->evaluate('string(spreadsheet:Cell[2])', $row),
'CodigoDoProjeto' => $xpath->evaluate('string(spreadsheet:Cell[3])', $row)
];
}
var_dump($records);
Output:
array(3) {
[1]=>
array(3) {
["Mercado"]=>
string(11) "Mineração"
["Segmento"]=>
string(10) "Portuário"
["CodigoDoProjeto"]=>
string(0) ""
}
[2]=>
array(3) {
["Mercado"]=>
string(10) "Portuário"
["Segmento"]=>
string(0) ""
["CodigoDoProjeto"]=>
string(10) "Greenfield"
}
[3]=>
array(3) {
["Mercado"]=>
string(0) ""
["Segmento"]=>
string(10) "Greenfield"
["CodigoDoProjeto"]=>
string(30) "Large CapEx>>maior que 500MBRL"
}
}
//spreadsheet:Table fetch any Table, (//spreadsheet:Table)[1] limits this to the first, (//spreadsheet:Table)[1]/spreadsheet:Row returns the Row elements of the first Table.
spreadsheet:Cell[1] returns the first Cell and string(spreadsheet:Cell[1]) returns the text content of it. If it did not match a node, it will return an empty string.
You can access just the first table in the tables array by doing $tablas[0]. Now you don't even need a foreach loop.
<?php
$tabelas = $arquivo->getElementsByTagName("Table");
$tablea = $tabelas[0];
$rows = $tablea->getElementsByTagName("Row");
$contRow = 1;
foreach ($rows as $row) {
if ($contRow > 1) {
$Mercado = $row->getElementsByTagName("Data")->item(0)->nodeValue;
$Segmento = $row->getElementsByTagName("Data")->item(1)->nodeValue;
$CodigoDoProjeto = $row->getElementsByTagName("Data")->item(2)->nodeValue;
}
$contRow++;
}
?>

How to insert dynamic row in different html table position

I want to echo following xml data in html table using php,
<type>Debit</type>
<item>Item-1</item>
<price>150</price>
<type>Debit</type>
<item>Item-2</item>
<price>250</price>
<type>Debit</type>
<item>Item-3</item>
<price>100</price>
type>Debit</type>
<item>Item-4</item>
<price>200</price>
............
...so on
<type>Credit</type>
<item>Item-50</item>
<price>200</price>
<type>Credit</type>
<item>Item-51</item>
<price>300</price>
<type>Credit</type>
<item>Item-60</item>
<price>100</price>
............
...so on
here is my Html table structure before inserting data
Now if you see my xml data, i want to dynamically insert debit items under Debit List and credit items in Credit List like the following sample:
I know how to add rows dynamically using insertRow(), getElementById("id").But in this case i need to maintain two different position [1 for debit, 1 for credit] so that i can insert them in correct position.Moreover rows are dynamic, so fixed id with fixed number of rows will not work i guess.How can i do this in php.Please share your idea to solve it in a vary simple way.Please let me know for any further information.Thanks
try this solution. If you use javascript, you use two tables and use append() element row in tbody.
<!DOCTYPE html>
<html>
<body>
<?php
$myXMLData =
"
<data>
<row>
<type>Debit</type>
<item>Item-1</item>
<price>150</price>
</row>
<row>
<type>Debit</type>
<item>Item-2</item>
<price>250</price>
</row>
<row>
<type>Debit</type>
<item>Item-3</item>
<price>100</price>
</row>
<row>
<type>Debit</type>
<item>Item-4</item>
<price>200</price>
</row>
<row>
<type>Credit</type>
<item>Item-50</item>
<price>200</price>
</row>
<row>
<type>Credit</type>
<item>Item-51</item>
<price>300</price>
</row>
<row>
<type>Credit</type>
<item>Item-60</item>
<price>100</price>
</row>
</data>";
$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object");
$debit = array();
$credit = array();
$debit_total = $credit_total = 0;
foreach ($xml as $row) {
if($row->type == 'Debit') {
$debit[] = array('item'=> (string)$row->item, 'price'=> (float)$row->price);
$debit_total += (float)$row->price;
} else {
$credit[] = array('item'=> (string)$row->item, 'price'=> (float)$row->price);
$credit_total += (float)$row->price;
}
}
?>
<table>
<tr>
<td colspan="2">Debit list<td>
</tr>
<?php
foreach ($debit as $key => $value) {
echo "<tr><td>{$value['item']}</td><td>{$value['price']}</td></tr>";
}
echo "<tr><td>Total</td><td>{$debit_total}</td></tr>";
echo "<tr><td colspan='2'></td></tr>";
echo "<tr><td colspan='2'>Credit list</td></tr>";
foreach ($credit as $key => $value) {
echo "<tr><td>{$value['item']}</td><td>{$value['price']}</td></tr>";
}
echo "<tr><td>Total</td><td>{$credit_total}</td></tr>";
echo "<tr><td colspan='2'></td></tr>";
?>
</table>
</body>
</html>

Retrieving data from siblings to node that match

I'm iterating through a xml doc with SimpleXML. I have an array ($ids) with id's and I'm checking whether there's a match or not in the XML (Worksheet/Table/Row/Cell/Data). If it's a match I want to be able to get to the data from the following two siblings, but I can't figure out how.
from the php:
// $ids <---- array('8', '53', '38')
foreach ($thePositions->Worksheet->Table->Row as $row) {
if($row->Cell->Data == true) {
for ($i = 0; $i < count($ids); $i++) {
foreach($row->Cell->Data as $data) {
if ($data == $ids[$i]) {
echo 'match!';
/*
Tried $siblings = $data->xpath('preceding-sibling::* | following-sibling::*');
but doesn't seem to work in this case.
*/
}
}
}
}
}
the xml:
<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<LastAuthor>Herpa Derp </LastAuthor>
<Created>2012-09-25T13:44:01Z</Created>
<LastSaved>2012-09-25T13:48:24Z</LastSaved>
<Version>14.0</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>14060</WindowHeight>
<WindowWidth>25040</WindowWidth>
<WindowTopX>25540</WindowTopX>
<WindowTopY>4100</WindowTopY>
<Date1904/>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="12" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s62">
<Font ss:FontName="Courier" ss:Color="#000000"/>
</Style>
</Styles>
<Worksheet ss:Name="Workbook1.csv">
<Table ss:ExpandedColumnCount="5" ss:ExpandedRowCount="79" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="65" ss:DefaultRowHeight="15">
<Column ss:Index="2" ss:AutoFitWidth="0" ss:Width="43"/>
<Column ss:AutoFitWidth="0" ss:Width="113"/>
<Column ss:Index="5" ss:AutoFitWidth="0" ss:Width="220"/>
<Row ss:Index="6">
<Cell ss:Index="3" ss:StyleID="s62"/>
</Row>
<Row>
<Cell ss:Index="3" ss:StyleID="s62"/>
</Row>
<Row>
<Cell ss:Index="3" ss:StyleID="s62"/>
</Row>
<Row>
<Cell ss:Index="2"><Data ss:Type="String">id</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">latitude</Data></Cell>
<Cell><Data ss:Type="String">longitude</Data></Cell>
</Row>
<Row>
<Cell ss:Index="2"><Data ss:Type="Number">8</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="Number">57.4999</Data></Cell> // to be saved to $latutude
<Cell><Data ss:Type="Number">15.8280</Data></Cell> // to be saved to $longitude
</Row>
<Row>
<Cell ss:Index="2"><Data ss:Type="Number">38</Data></Cell>
<Cell><Data ss:Type="Number">56.5659</Data></Cell>
<Cell><Data ss:Type="Number">16.1380</Data></Cell>
</Row>
The reason asking for siblings isn't working is that the <Data> elements aren't siblings; they're more like cousins - children of adjacent <Cell> elements.
For the same reason, you shouldn't be using foreach($row->Cell->Data as $data), as this is equivalent to foreach($row->Cell[0]->Data as $data), i.e. look at all <Data> children of the first <Cell> node. Since there will only ever be one <Data> element in a <Cell>, you might as well just write $data = $row->Cell[0]->Data - which in this case is fine, because the values you're looking for are at the beginning of rows.
What you actually need to do is loop over the <Cell>s: foreach($row->Cell as $cell) { $data = $cell->Data; /* ... */ }
You then have a couple of options for finding the adjacent cells, including XPath. A more "PHP-ish" way would be to use array indexes (siblings are indexed numerically in SimpleXML loop/array access):
foreach($row->Cell as $cell_index => $cell)
{
$data = $cell->Data;
if ($data == $ids[$i])
{
// Tip: always cast SimpleXML objects to string when you're done with their magic XMLiness
$latitudes[$i] = (string)$row->Cell[ $cell_index + 1 ]->Data;
$longitudes[$i] = (string)$row->Cell[ $cell_index + 2 ]->Data;
}
}
Alternatively, you could rely on your IDs always being in the first column, and the lat and long in the next two (this is a spreadsheet, after all!) and avoid the inner loop altogether:
if ( $row->Cell[0]->Data == $ids[$i] )
{
$latitudes[$i] = (string)$row->Cell[1]->Data;
$longitudes[$i] = (string)$row->Cell[2]->Data;
}
It looks like in case of this XML, cells are always in same order so it can be done as follows:
$ids = array('8', '53', '38');
foreach ($xml->Worksheet->Table->Row as $row) {
$children = $row->children();
if (count($children) == 3 && in_array(((string) $children[0]->Data), $ids)) {
echo 'lat: ' . $children[1]->Data . ' lng: ' . $children[2]->Data . "\n";
}
}
You can do it entirely in XPath, without any loops, like:
//Row[Cell/Data[. = '8' or . = '53' or . = '38']]/following-sibling::*[position() <= 2]
That search all rows with the id in any data element and then takes the next two siblings.
Or
//Row[Cell[1]/Data[. = '8' or . = '53' or . = '38']]/following-sibling::*[position() <= 2]
if it is sure that the id is always in the first cell. (which also prevents errors due to an id being the same as a longitude/langitude)
Or
//Row[Cell[#ss:Index = "2"]/Data[. = '8' or . = '53' or . = '38']]/following-sibling::*[position() <= 2]
if the id is in the cell with index 2.
But in all cases, you need to initialize the namespaces correctly
An alternative approach if you have lots of IDs to match is to create a "hash" of all the rows based on their ID, and then look into that hash rather than looping through searching for matches.
// Initialise an empty array to use as the hash
$rows_by_id = array();
// Loop over all the rows in the spreadsheet
foreach ($thePositions->Worksheet->Table->Row as $row) {
// Skip rows with less than 3 cells
if ( count($row->Cell) < 3 ) {
continue;
}
// Take the ID from the first cell on the row
$id = (string)$row->Cell[0]->Data;
// Add this row to the hash, identified by it's ID
$rows_by_id[$id] = array(
'latitude' => (string)$row->Cell[1]->Data,
'longitude' => (string)$row->Cell[2]->Data
);
// Or if your IDs are not unique, and you want all matches:
// $rows_by_id[$id][] = array( ... )
}
foreach ( $ids as $required_id ) {
// Retrieve the results from the hash
if ( isset($rows_by_id[$required_id]) ) {
$matched_row = $rows_by_id[$required_id];
echo "ID $required_id has latitude {$matched_row['latitude']} and longitude {$matched_row['longitude']}.\n";
}
else {
echo "ID $required_id was not matched. :(\n";
}
// If you have non-unique matches, you'll need something like this:
// $all_matched_rows = $rows_by_id[$required_id]; ... foreach ( $all_matched_rows as $matched_row )
}

Exporting defined elements from XML

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?

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.

Categories