display only nodes in XML with specific attributes in php [duplicate] - php

This question already has answers here:
Using XPath, How do I select a node based on its text content and value of an attribute?
(2 answers)
Closed 9 years ago.
if i have:
<listing>
<name>Bob</name>
<age>20</age>
<hair>red</hair>
</listing>
<listing>
<name>John</name>
<age>24</age>
<hair>black</hair>
</listing>
how do i code my php page to only display listings if hair = black
so that it would only pull in
<listing>
<name>John</name>
<age>24</age>
<hair>black</hair>
</listing>
Thanks

Use XPath.
// First, your XML must be wraped with some root tag.
$data = <<<XML
<root>
<listing>
<name>Bob</name>
<age>20</age>
<hair>red</hair>
</listing>
<listing>
<name>John</name>
<age>24</age>
<hair>black</hair>
</listing>
</root>
XML;
// Instancing the SimpleXMLElement within the XML(obviously)
$xml = new SimpleXMLElement($data);
// XPath
$xpath = $xml->xpath("listing[contains(hair,'black')]");
/**
* eXplained XPath:
* <listing> that
* <hair>'s content is equal black
*/
foreach($xpath as $node){
// just for fun echoes the <results> node
echo $node->asXml();
}

Related

PHP XML: Getting text of a node and its children

I know that this questions has been asked before, but I cannot make it work. I'm using simplexml and xpath in a PHP file. I need to get text from a node including the text in its child nodes. So, the results should be:
Mr.Smith bought a white convertible car.
Here is the xml:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test9.xsl"?>
<items>
<item>
<description>
<name>Mr.Smith bought a <car>white</car> <car>convertible</car> car.</name>
</description>
</item>
</items>
The php that's not working is:
$text = $xml->xpath('//items/item/description/name');
foreach($text as &$value) {
echo $value;
}
Please help!
To get the node value with all its child elements, you can use DOMDocument, with C14n():
<?php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test9.xsl"?>
<items>
<item>
<description>
<name>Mr.Smith bought a <car>white</car> <car>convertible</car> car.</name>
</description>
</item>
</items>
XML;
$doc = new DOMDocument;
$doc->loadXML($xml);
$x = new DOMXpath($doc);
$text = $x->query('//items/item/description/name');
echo $text[0]->C14n(); // Mr.Smith bought a white convertible car.
Demo

Append / Clone nodes in xml File with help of php

I am beginner in php and currently working on php-xml parsing in which i am not getting how should i append node with specific node having different value in same xml file.
Explanation :
Since i don't have enough data so i need to duplicate nodes (here it is test node) so that i can increase my file size and then work on parsing.
In short i need to generate big xml file with exisitng single node.
Current Xml File :
<?xml version="1.0" encoding="utf-8"?>
<Testings xmlns="http://rets.org/xsd/Syndication/2012-03" xmlns:Level="http://rets.org/xsd/RETSLevel" xmlns:schemaLocation="http://rets.org/xsd/Syndication/2012-03/Syndication.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="0.96" versionTimestamp="2012-02-07T03:00:00Z" xml:lang="en-us">
<Test>
<Area>
<Level:preference-order>1</Level:preference-order>
<Level:address-preference-order>1</Level:address-preference-order>
<Level:FullStreetAddress>2245 Don Knotts Blvd.</Level:FullStreetAddress>
<Level:UnitNumber>2</Level:UnitNumber>
<Level:City>Morgantown</Level:City>
<Level:StateOrProvince>WV</Level:StateOrProvince>
<Level:PostalCode>26501</Level:PostalCode>
<Level:Country>true</Level:Country>
</Area>
<AreaPrice Level:isgSecurityClass="Public">234000</ListPrice>
<AreaPriceLow Level:isgSecurityClass="Public">214000</ListPriceLow>
<AreaPrices>
<AreaPrice>
<AreaListPrice Level:currencyCode="AUS" Level:isgSecurityClass="Public">483999.0</AreaListPrice>
<AreaListPriceLow Level:currencyCode="EUR" Level:isgSecurityClass="Public">470000.0</AreaListPriceLow>
</AreaPrice>
</AreaPrices>
</Test>
</Testings>
Expected Output:
<?xml version="1.0" encoding="utf-8"?>
<Testings xmlns="http://rets.org/xsd/Syndication/2012-03" xmlns:Level="http://rets.org/xsd/RETSLevel" xmlns:schemaLocation="http://rets.org/xsd/Syndication/2012-03/Syndication.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="0.96" versionTimestamp="2012-02-07T03:00:00Z" xml:lang="en-us">
<Test>
<Area>
<Level:preference-order>1</Level:preference-order>
<Level:address-preference-order>1</Level:address-preference-order>
<Level:FullStreetAddress>2245 Don Knotts Blvd.</Level:FullStreetAddress>
<Level:UnitNumber>2</Level:UnitNumber>
<Level:City>Morgantown</Level:City>
<Level:StateOrProvince>WV</Level:StateOrProvince>
<Level:PostalCode>26501</Level:PostalCode>
<Level:Country>true</Level:Country>
</Area>
<AreaPrice Level:isgSecurityClass="Public">234000</ListPrice>
<AreaPriceLow Level:isgSecurityClass="Public">214000</ListPriceLow>
<AreaPrices>
<AreaPrice>
<AreaListPrice Level:currencyCode="AUS" Level:isgSecurityClass="Public">483999.0</AreaListPrice>
<AreaListPriceLow Level:currencyCode="EUR" Level:isgSecurityClass="Public">470000.0</AreaListPriceLow>
</AreaPrice>
</AreaPrices>
</Test>
<Test>
<Area>
<Level:preference-order>1</Level:preference-order>
<Level:address-preference-order>1</Level:address-preference-order>
<Level:FullStreetAddress>2245 Don Knotts Blvd.</Level:FullStreetAddress>
<Level:UnitNumber>2</Level:UnitNumber>
<Level:City>Morgantown</Level:City>
<Level:StateOrProvince>WV</Level:StateOrProvince>
<Level:PostalCode>26501</Level:PostalCode>
<Level:Country>true</Level:Country>
</Area>
<AreaPrice Level:isgSecurityClass="Public">15000</ListPrice>
<AreaPriceLow Level:isgSecurityClass="Public">214000</ListPriceLow>
<AreaPrices>
<AreaPrice>
<AreaListPrice Level:currencyCode="AUS" Level:isgSecurityClass="Public">483999.0</AreaListPrice>
<AreaListPriceLow Level:currencyCode="EUR" Level:isgSecurityClass="Public">470000.0</AreaListPriceLow>
</AreaPrice>
</AreaPrices>
</Test>
</Testings>
**My Approach **
$xmlString = simplexml_load_string(file_get_contents('./PhpXmlFile.xml'));
$xmlString = $xmlString->xpath('/Testings/Test/');
if ( $xmlString && is_array( $xmlString ) ) {
// since it IS an array, set to the first element of the array
$xmlString = $xmlString[0];
// And NOW we can append
$xmlString = $xmlString->addChild('Test','');
}
$dom = new DOMDocument("1.0");
$dom->preserveWhiteSpace = true;
$dom->formatOutput = true;
$dom->loadXML($xmlString->saveXML());
Thanks in advance!!
Here we are using DOMDocument for cloning a child node. Here for an example i am using nodeValue as 1000 you can change it to the value you want.
Here in a below code we are using $domDocument->getElementsByTagName("AreaPrice")->item(2)->nodeValue=1000; for item no 2 because after appending the a clone node, There will be four elements with name AreaPrice.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string = <<<HTML
<?xml version="1.0" encoding="utf-8"?>
<Testings xmlns="http://rets.org/xsd/Syndication/2012-03" xmlns:Level="http://rets.org/xsd/RETSLevel" xmlns:schemaLocation="http://rets.org/xsd/Syndication/2012-03/Syndication.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="0.96" versionTimestamp="2012-02-07T03:00:00Z" xml:lang="en-us">
<Test>
<Area>
<Level:preference-order class='sas'>1</Level:preference-order>
<Level:address-preference-order>1</Level:address-preference-order>
<Level:FullStreetAddress>2245 Don Knotts Blvd.</Level:FullStreetAddress>
<Level:UnitNumber>2</Level:UnitNumber>
<Level:City>Morgantown</Level:City>
<Level:StateOrProvince>WV</Level:StateOrProvince>
<Level:PostalCode>26501</Level:PostalCode>
<Level:Country>true</Level:Country>
</Area>
<AreaPrice Level:isgSecurityClass="Public">234000</AreaPrice>
<AreaPriceLow Level:isgSecurityClass="Public">214000</AreaPriceLow>
<AreaPrices>
<AreaPrice>
<AreaListPrice Level:currencyCode="AUS" Level:isgSecurityClass="Public">483999.0</AreaListPrice>
<AreaListPriceLow Level:currencyCode="EUR" Level:isgSecurityClass="Public">470000.0</AreaListPriceLow>
</AreaPrice>
</AreaPrices>
</Test>
</Testings>
HTML;
$domDocument = new DOMDocument();
$domDocument->loadXML($string);
$results=$domDocument->getElementsByTagName("Test");
$clonedNode=$results->item(0)->cloneNode(true);
$results->item(0)->parentNode->appendChild($clonedNode);
$domDocument->getElementsByTagName("AreaPrice")->item(2)->nodeValue=1000;//change the value you want.
echo $domDocument->saveXML();

How to get XML-Namespace-Attribute with SimpleXml? [duplicate]

This question already has an answer here:
Simplexml get attributes with a namespace
(1 answer)
Closed 8 years ago.
I try to use the Immobilienscout24-API to get Offerings of my Customer to his homepage.
I get an XML-Result with the form like here.
After parsing this result with
$obj = new SimpleXMLElement($myXmlString);
the attribute xsi:type of the element realEstateElement is lost. How can I access that element?
Thanks!
<?php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<realestates:realEstates xmlns:ns2="http://rest.immobilienscout24.de/schema/platform/gis/1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:common="http://rest.immobilienscout24.de/schema/common/1.0" xmlns:realestates="http://rest.immobilienscout24.de/schema/offer/realestates/1.0">
<Paging>
<next xlink:href="http://rest.sandbox-immobilienscout24.de/restapi/api/offer/v1.0/user/me/realestate?pagenumber=2"/>
<pageNumber>1</pageNumber>
<pageSize>20</pageSize>
<numberOfPages>12</numberOfPages>
<numberOfHits>222</numberOfHits>
</Paging>
<realEstateList>
<realEstateElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:offerlistelement="http://rest.immobilienscout24.de/schema/offer/listelement/1.0" xsi:type="offerlistelement:OfferHouseRent" xlink:href="http://rest.sandbox-immobilienscout24.de/restapi/api/offer/v1.0/user/me/realestate/62283354" modification="2013-12-05T09:13:45.000+01:00" creation="2013-12-05T09:13:37.000+01:00" publishDate="2013-12-07T20:37:38.333+01:00" id="62283354">
<externalId>62283354</externalId>
<title>Unterbach Haus</title>
<address>
<street>Eichenwand</street>
<houseNumber>2</houseNumber>
<postcode>40627</postcode>
<city>Düsseldorf</city>
<geoHierarchy>
<continent>
<geoCodeId>1</geoCodeId>
<fullGeoCodeId>1</fullGeoCodeId>
</continent>
<country>
<geoCodeId>276</geoCodeId>
<fullGeoCodeId>1276</fullGeoCodeId>
</country>
<region>
<geoCodeId>10</geoCodeId>
<fullGeoCodeId>1276010</fullGeoCodeId>
</region>
<city>
<geoCodeId>12</geoCodeId>
<fullGeoCodeId>1276010012</fullGeoCodeId>
</city>
<quarter>
<geoCodeId>57</geoCodeId>
<fullGeoCodeId>1276010012057</fullGeoCodeId>
</quarter>
<neighbourhood>
<geoCodeId>5111000008401</geoCodeId>
</neighbourhood>
</geoHierarchy>
</address>
<realEstateState>ACTIVE</realEstateState>
<common:publishChannels>
<publishChannel id="10000" title="Immobilienscout24"/>
<publishChannel id="10001" title="Homepage"/>
</common:publishChannels>
<price>
<value>45444</value>
<currency>EUR</currency>
</price>
<livingSpace>440</livingSpace>
<plotArea>460</plotArea>
<numberOfRooms>5</numberOfRooms>
<builtInKitchen>false</builtInKitchen>
</realEstateElement>
</realEstateList>
</realestates:realEstates>
XML;
$sxml = new SimpleXMLElement($xml);
var_dump($sxml->realEstateList->realEstateElement->attributes('xsi', true)->type);
http://www.php.net/manual/en/simplexmlelement.attributes.php Comments might be useful ;)

PHP XPath - Select one specific node and his parent node with one rule [duplicate]

This question already has answers here:
How do I select multiple sets of attributes within an XML document using XPath?
(2 answers)
Closed 9 years ago.
I'm working on parsing some XML files using Xpath. I want to select one specific Category node and his parent node (Sport) with one expression?!
XML document: livescore_full.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
<LivescoreData generatedAt="2013-08-18T09:41:19 CEST" type="full">
<Sport SportId="1">
<Name language="en">Soccer</Name>
<Category CategoryId="48">
<Name language="en">Argentina</Name>
<Tournament TournamentId="68" UniqueTournamentId="155">
<Name language="en">Primera Division, Torneo Inicial</Name>
</Tournament>
</Category>
<Category CategoryId="49">
<Name language="en">Brazil</Name>
<Tournament TournamentId="69" UniqueTournamentId="156">
<Name language="en">Brazil Primera Division</Name>
</Tournament>
</Category>
</Sport>
<Sport SportId="2">
<Name language="en">Basketball</Name>
<Category CategoryId="55">
<Name language="en">Spain</Name>
<Tournament TournamentId="545" UniqueTournamentId="453">
<Name language="en">Primera Division, Torneo Inicial</Name>
</Tournament>
</Category>
<Category CategoryId="56">
<Name language="en">England</Name>
<Tournament TournamentId="654" UniqueTournamentId="5656">
<Name language="en">England</Name>
</Tournament>
</Category>
</Sport>
</LivescoreData>
$xml = simplexml_load_file("livescore_full.xml");
$data = $xml->xpath('//Category[#CategoryId="48"]');
print_r($data); // only return data for specific Category node
$data contains data about Category node id=48, but there is no information about the sport id and sport name.
How to write Xpath rule to select Sport and Category data using CategoryId attribute? I tried some variations with the ancestor-or-self, but without success.
"/LiveScoreData/Sport[Category[#CategoryId='48']"
Would be one way.
One easy answer which comes to mind is (How do I select multiple nodesets):
$data = $xml->xpath('//Category[#CategoryId="48"] | //Sport[Category[#CategoryId="48"]]');
However, I'd drop SimpleXML for this case and do this:
$dom = new DOMDocument();
$dom->load("livescore_full.xml");
$xpath = new DOMXPath($dom);
$category = $xpath->query('//Category[#CategoryId="48"]')->item(0);
//then sport is just it's parentNode:
$sport = $category->parentNode;
//removing all other cats:
$dom = new DOMDocument();
$dom->load("livescore_full.xml");
$xpath = new DOMXPath($dom);
$categories = $xpath->query('//Sport[Category[#CategoryId="48"]]/Category[#CategoryId!="48"]');
while($othercat = $categories->item(0)){
$othercat->parentNode->removeChild($othercat);
}

How to get the title? From this XML using Zend framework?

I am very new to Zend Framework. And i have tried to get the XML value but cant make it work.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<result count="2">
<blocks>
<listing>
<title>Title 1</title>
<id>1</id>
</listing>
<listing>
<title>Title 2</title>
<id>2</id>
</listing>
</blocks>
</result>
PHP (to find all the title):
$dom = new Zend_Dom_Query();
$dom->setDocumentXml($result);
$results = $dom->queryXpath('/result/blocks/listing/title');
//$dom->queryXpath('/*/*/listing'); no luck
//$dom->queryXpath('///listing'); no luck
foreach($results as $k)
{
Zend_Debug::dump($k->getAttribute('title')); // empty
echo $k->getDocument(); // shows none
}
Any help?
Using queryXpath('/result/blocks/listing/title') your $k already is the DOMElement that represents the <tile>...</title> elements.
You can retrieve the value via $k->nodeValue. For a DOMElement that's the concatenation of all text nodes in the descendant axis.
foreach($results as $k)
{
Zend_Debug::dump($k->nodeValue); // empty
}
title is a node - not an attribute - attributes are placed inside the tag :)

Categories