I'm having trouble getting a value from an xml string.
$query_return_string contains the following as a string:
<?xml version="1.0" encoding="UTF-8"?>
<sparql xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result">
<head>
<variable name="label"/>
</head>
<results>
<result>
<label>Demo sports 3</label>
</result>
</results>
</sparql>
This is the php code:
<?php
if ($query_return_string) {
$query_return_dom = DOMDocument::loadXML($query_return_string);
$xpath_dom = new DOMXPath($query_return_dom);
$xpath = '//label';
$entries = $xpath_dom->query($xpath);
dsm($entries->length);
// /sparql/results/result/label
}
?>
I'm not even bothering trying to go over the object to get the value because $entries->length returns 0.
What could I be doing wrong? This seems pretty basic.
You need to register the namespace.
$xpath_dom->registerNamespace('sw', 'http://www.w3.org/2001/sw/DataAccess/rf1/result');
$xpath = '//sw:label';
etc.
Related
I load the following XML data into SimpleXML like this:
<?php
$xmlString = <<<'XML'
<?xml version="1.0"?>
<response>
<item key="0">
<title>AH 2308</title>
<field_a>3.00</field_a>
<field_b>7.00</field_b>
<field_d1>35.00</field_d1>
<field_d2>40.00</field_d2>
<field_e></field_e>
<field_g2></field_g2>
<field_g>M 45x1,5</field_g>
<field_gewicht>0.13</field_gewicht>
<field_gtin>4055953012781</field_gtin>
<field_l>40.00</field_l>
<field_t></field_t>
<field_abdrueckmutter>KM 9</field_abdrueckmutter>
<field_sicherung>MB 7</field_sicherung>
<field_wellenmutter>KM 7</field_wellenmutter>
</item>
<item key="1">
<title></title>
<field_a></field_a>
<field_b></field_b>
<field_d1></field_d1>
<field_d2></field_d2>
<field_e></field_e>
<field_g2></field_g2>
<field_g></field_g>
<field_gewicht></field_gewicht>
<field_gtin></field_gtin>
<field_l></field_l>
<field_t></field_t>
<field_abdrueckmutter></field_abdrueckmutter>
<field_sicherung></field_sicherung>
<field_wellenmutter></field_wellenmutter>
</item>
</response>
XML;
$xml = simplexml_load_string($xml);
How can I achieve the following result:
<?xml version="1.0"?>
<response>
<item key="0">
<title>AH 2308</title>
<field_a>3.00</field_a>
<field_b>7.00</field_b>
<field_d1>35.00</field_d1>
<field_d2>40.00</field_d2>
<field_e></field_e>
<field_g2></field_g2>
<field_g>M 45x1,5</field_g>
<field_gewicht>0.13</field_gewicht>
<field_gtin>4055953012781</field_gtin>
<field_l>40.00</field_l>
<field_t></field_t>
<field_abdrueckmutter>KM 9</field_abdrueckmutter>
<field_sicherung>MB 7</field_sicherung>
<field_wellenmutter>KM 7</field_wellenmutter>
</item>
<item key="1"></item>
</response>
To delete all empty elements, I could use the following working code:
foreach ($xml->xpath('/child::*//*[not(*) and not(text()[normalize-space()])]') as $emptyElement) {
unset($emptyElement[0]);
}
But that's not exactly what I want.
Basically, when the <title> element is empty, I want to remove it with all its siblings and keep the parent <item> element.
What's important: I also want to keep empty element, if the <title> is not empty. See <item key="0"> for example. The elements <field_e>, <field_g2> and <field_t>will be left untouched.
Is there an easy xpath query which can achieve that? Hope anyone can help. Thanks in advance!
This xpath query is working:
foreach ($xml->xpath('//title[not(text()[normalize-space()])]/following-sibling::*') as $emptyElement) {
unset($emptyElement[0]);
}
It keeps the <title> element but I can live with that.
DOM is more flexible manipulating nodes:
$document = new DOMDocument();
$document->loadXML($xmlString);
$xpath = new DOMXpath($document);
$expression = '/response/item[not(title[normalize-space()])]';
foreach ($xpath->evaluate($expression) as $emptyItem) {
// replace children with an empty text node
$emptyItem->textContent = '';
}
echo $document->saveXML();
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();
I have this xml content :
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ns3:searchResult total="1" xmlns:ns5="ers.ise.cisco.com" xmlns:ers-v2="ers-v2" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns3="v2.ers.ise.cisco.com">
<ns3:resources>
<ns5:resource id="42e98860-cb88-11e5-9b0c-000c29c658fa" name="11:22:33:44:55:66">
<link rel="self" href="https://1.2.3.4:9060/ers/config/endpoint/42e98860-cb88-11e5-9b0c-000c29c658fa" type="application/xml"/>
</ns5:resource>
</ns3:resources>
</ns3:searchResult>
i need to get the value of the ns5:resource id value (42e98860-cb88-11e5-9b0c-000c29c658fa), but the use of namespaces in every node has me confused, i tried using the $xml->children('ns5',true)->resource->id and everything i try just gives me empty simplexml objects.
Any suggestions?
I think you can use this xpath expression:
$elements =
$xml->xpath('/ns3:searchResult/ns3:resources/ns5:resource');
xpath will return an array and you can take the first item from that array. That item is of type SimpleXMLElement and you can get your id from its attributes.
$source = <<<SOURCE
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ns3:searchResult total="1" xmlns:ns5="ers.ise.cisco.com" xmlns:ers-v2="ers-v2" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns3="v2.ers.ise.cisco.com">
<ns3:resources>
<ns5:resource id="42e98860-cb88-11e5-9b0c-000c29c658fa" name="11:22:33:44:55:66">
<link rel="self" href="https://1.2.3.4:9060/ers/config/endpoint/42e98860-cb88-11e5-9b0c-000c29c658fa" type="application/xml"/>
</ns5:resource>
</ns3:resources>
</ns3:searchResult>
SOURCE;
$xml = simplexml_load_string($source);
$elements = $xml->xpath('/ns3:searchResult/ns3:resources/ns5:resource');
$element = $elements[0];
echo $element->attributes()->id->__toString();
Will result in:
42e98860-cb88-11e5-9b0c-000c29c658fa
I have a php variable that contains xml code. I would like to get only one value from that xml and go along.
The xml is:
<?xml version="1.0" encoding="UTF-8" ?> <response> <status>SUCCESS</status> <data><count>1</count> <subscriberlist> <item> <subscriberid>4</subscriberid> <emailaddress>bbbbbb#bbbbbb.bb</emailaddress> <format>h</format> <subscribedate>1314903006</subscribedate> <confirmed>1</confirmed> <unsubscribed>0</unsubscribed> <bounced>0</bounced> <listid>3</listid> </item> </subscriberlist></data></response>
I would like to create the var $subscriberid and get the value (in this case 4)
Can someone explain me?
This doc on php.net will tell you how: http://php.net/manual/en/simplexml.examples-basic.php
For the following xml:
<?xml version="1.0" encoding="UTF-8" ?>
<response>
<status>SUCCESS</status>
<data>
<count>1</count>
<subscriberlist>
<item>
<subscriberid>4</subscriberid>
<emailaddress>bbbbbb#bbbbbb.bb</emailaddress>
<format>h</format>
<subscribedate>1314903006</subscribedate>
<confirmed>1</confirmed>
<unsubscribed>0</unsubscribed>
<bounced>0</bounced>
<listid>3</listid>
</item>
</subscriberlist>
</data>
</response>
If the xml was inside of $xmlstr then to get the subscriberid you would need the following php code:
<?php
$xml = new SimpleXMLElement($xmlstr);
$subscriberid = $xml->data->subscriberlist->item->subscriberid;
?>
You can use simplexml_load_string then parse the data
<?php
$xml = simplexml_load_string('<?xml vers...');
$subscriberid = $xml->data->subscriberlist->item->subscriberid;
?>
Say I have this following XML structure:
<?xml version="1.0" encoding="UTF-8"?>
<main>
<parent>
<child1>some value</child1>
<child2>another value</child2>
</parent>
</main>
I made a variable of the XML and now I want to get the values of child1, so I use SimpleXML:
$xml = new SimpleXMLElement($xml);
$this->xmlcode = (string) $xml->main->parent->child1;
But I get this message: Notice: Trying to get property of non-object in /x.php on line x
I also tried it with $xml->parent->child1, but no success.
Anyone??
$xml = new SimpleXMLElement($xml);
$this->xmlcode = (string) $xml->parent[0]->child1;
A good example of using XPath with php for the SimpleXMLElement can be found here
http://www.php.net/manual/en/class.simplexmlelement.php#95229
// Find the topmost element of the domDocument
$xpath = new DOMXPath($xml);
$child1 = $xpath->evaluate('/main/parent/child1')->item(0);
Variant for xpath (Also how to get content of node having dashes in name):
<?xml version="1.0" encoding="UTF-8"?> <main>
<parent>
<child-1>some value</child-1>
<child-2>another value</child-2>
</parent> </main>
$xml = simplexml_load_string($content);
$node_value= (string)$xml->xpath('parent/child-1')[0];
result of $node_value:
"some value"