i have the following xml
<HEAD>
<Body attribute3="3" attribute1="1" attribute2="2">
<Response id="4" status="OK">
<Token c="c" b="b" a="a" e="e" d="d">
</Token>
</Response>
</Body>
</HEAD>
and i used simplexml to manipulate it.The problem is that i have to Canonicalize XML nodes which is supported by DOMDocument.
With simplexml i used $xml->Body->Response->asXML(); to get the Response node as XML.
I am trying to get Response node as XML with DOMDocument but i don't know how
so far i have
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($Response);
$xmlDoc->C14N();
$xmlDoc->formatOutput = true;
$xml_string = $xmlDoc->saveXML();
an in $xml_string i have the xml
i want to get Response node as XML with DOMDocument
<Response id="4" status="OK">
<Token c="c" b="b" a="a" e="e" d="d">
</Token>
</Response>
can anyone help me how to do that
Any help appreciated
If I understand you correctly, this should get you there:
$XMLDoc = new DOMDocument();
$XMLDoc->loadXML($response);
$xpath = new DOMXPath($XMLDoc);
$target = $xpath->query('//Response');
echo $XMLDoc->saveXML($target[0]);
Output:
<Response id="4" status="OK">
<Token c="c" b="b" a="a" e="e" d="d">
</Token>
</Response>
Related
Considering this XML
<?xml version="1.0" encoding="UTF-8"?>
<Assertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:tc:SAML:2.0:assertion http://docs.oasis-open.org/security/saml/v2.0/saml-schema-assertion-2.0.xsd" ID="_a75adf55-01d7-40cc-929f-dbd8372ebdfc" IssueInstant="2009-09-09T00:46:02Z" Version="2.0">
<Subject>
<NameID>801234567890</NameID>
</Subject>
....
</Assertion>
PHP
$dom = new DOMDocument();
$ret = $dom->loadXML($data);
$xp = new DOMXPath($dom);
$node_list = $xp->query('/Assertion');
$node_list->length return 0 element. I want to extract the DOMElement but somehow it didn't work.
As stated on the comments by Sami Kuhmonen you may need to register the namespaces, here is an example:
<?php
$string= <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<Assertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:tc:SAML:2.0:assertion http://docs.oasis-open.org/security/saml/v2.0/saml-schema-assertion-2.0.xsd" ID="_a75adf55-01d7-40cc-929f-dbd8372ebdfc" IssueInstant="2009-09-09T00:46:02Z" Version="2.0">
<Subject>
<NameID>801234567890</NameID>
</Subject>
</Assertion>
XML;
$dom = new DOMDocument();
$dom->loadXML($string);
$xp = new DOMXPath($dom);
// registering the namespaces
$xp->registerNamespace('a','urn:oasis:names:tc:SAML:2.0:assertion');
$xp->registerNamespace('b','http://www.w3.org/2001/XMLSchema-instance');
// using the prefix of the registered namespace in the xpath expression
$node_list = $xp->query('/a:Assertion');
print $node_list->length
?>
I am trying to parse XML with PHP. The XML is a response from ebay getsellerlist api, and is structured like so:
<!--?xml version="1.0" encoding="UTF-8"?-->
<getsellerlistresponse xmlns="urn:ebay:apis:eBLBaseComponents">
<timestamp>2016-08-11T14:17:39.869Z</timestamp>
<ack>Success</ack>
<version>967</version>
<build>E967_CORE_APISELLING_17965876_R1</build>
<itemarray>
<item>
<itemid>itemid1</itemid>
<listingdetails>
<viewitemurl>itemurl1</viewitemurl>
</listingdetails>
<primarycategory>
<categoryid>categoryid1</categoryid>
<categoryname>categoryname1</categoryname>
</primarycategory>
<title>title1</title>
<picturedetails>
<galleryurl>url1</galleryurl>
<photodisplay>thumbnail1</pictureurl>
<pictureurl>picture1</pictureurl>
</picturedetails>
</item>
</itemarray>
</getsellerlistresponse>
My php is as follows:
<?
$xml = '<!--?xml version="1.0" encoding="UTF-8"?--><getsellerlistresponse xmlns="urn:ebay:apis:eBLBaseComponents"><timestamp>2016-08-11T14:17:39.869Z</timestamp><ack>Success</ack><version>967</version><build>E967_CORE_APISELLING_17965876_R1</build><itemarray><item><itemid>itemid1</itemid><listingdetails><viewitemurl>itemurl1</viewitemurl></listingdetails><primarycategory><categoryid>categoryid1</categoryid><categoryname>categoryname1</categoryname></primarycategory><title>title1</title><picturedetails><galleryurl>url1</galleryurl><photodisplay>thumbnail1</pictureurl><pictureurl>picture1</pictureurl></picturedetails></item><item><itemid>itemid2</itemid><listingdetails><viewitemurl>itemurl2</viewitemurl></listingdetails><primarycategory><categoryid>categoryid2</categoryid><categoryname>categoryname2</categoryname></primarycategory><title>title1</title><picturedetails><galleryurl>url2</galleryurl><photodisplay>thumbnail2</pictureurl><pictureurl>picture2</pictureurl></picturedetails></item></itemarray></getsellerlistresponse>';
$dom = new DOMDocument();
$dom->loadXML($xml);
$title_nodes = $dom->getElementsByTagName('title');
$titles = array();
foreach ($title_nodes as $node) {
$titles[] = $node->nodeValue;
echo $node->nodeValue;
}
echo $titles[0];
echo count($titles);
?>
When I run it, I get a blank page, no errors, nothing.
If I check $titles length using count(), it comes back as zero.
For some reason it is not getting the title node (or any other nodes) and I can't figure out how to parse the xml string with php and get the node values.
Any help most appreciated, if the question is vague or lacking detail, please let me know and I will correct it.
The XML isn't valid:
Unable to parse any XML input. org.jdom2.input.JDOMParseException: Error on line 2: The element type "photodisplay" must be terminated by the matching end-tag "".
And that's only after you remove the comments in your XML declaration:
<!--?xml version="1.0" encoding="UTF-8"?-->
shoud be
<?xml version="1.0" encoding="UTF-8"?>
Working demo:
<?php
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<getsellerlistresponse xmlns="urn:ebay:apis:eBLBaseComponents">
<timestamp>2016-08-11T14:17:39.869Z</timestamp>
<ack>Success</ack>
<version>967</version>
<build>E967_CORE_APISELLING_17965876_R1</build>
<itemarray>
<item>
<itemid>itemid1</itemid>
<listingdetails>
<viewitemurl>itemurl1</viewitemurl>
</listingdetails>
<primarycategory>
<categoryid>categoryid1</categoryid>
<categoryname>categoryname1</categoryname>
</primarycategory>
<title>title1</title>
<picturedetails>
<galleryurl>url1</galleryurl>
<photodisplay>thumbnail1</photodisplay>
<pictureurl>picture1</pictureurl>
</picturedetails>
</item>
</itemarray>
</getsellerlistresponse>';
$dom = new DOMDocument();
$dom->loadXML($xml);
$title_nodes = $dom->getElementsByTagName('title');
$titles = array();
foreach ($title_nodes as $node) {
$titles[] = $node->nodeValue;
echo $node->nodeValue;
}
echo $titles[0];
echo count($titles);
How to write php so I know the link here?
There will always be different links
<response>
<redirect>
http://www.example.com/
</redirect>
<code>0</code>
<description>OK</description>
</response>
try simplexml
$xml ='<response>
<redirect>
http://www.example.com/
</redirect>
<code>0</code>
<description>OK</description>
</response>';
$xml = simplexml_load_string($xml);
echo $xml->redirect; // http://www.example.com/
simplexml_load_string
This should answer all your questions...
http://www.php.net/manual/en/function.simplexml-load-string.php
Use DOM+Xpath:
$xml = <<<'XML'
<response>
<redirect>http://www.example.com/</redirect>
<code>0</code>
<description>OK</description>
</response>
XML;
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
var_dump($xpath->evaluate('string(/response/redirect)'));
Output:
string(23) "http://www.example.com/"
I trying to parse XML file but getting parsing error.
Code ::
$xmlUrl = 'products.xml';
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
XML file ::
<?xml version="1.0" encoding="UTF-8"?>
<result>
<orderlist>
<order_no>123123</order_no>
<date></date>
<client_name>Knapp's Donut Shop</client_name>
<sector>54</sector>
</orderlist>
</result>
I am getting error because of this tag
<client_name>Knapp's Donut Shop</client_name>
The conversion to a SimpleXML Object and the output works, see code example below.
Check your "products.xml" file for the correct UTF-8 encoding type.
<?php
$xml = <<< XML
<?xml version="1.0" encoding="UTF-8"?>
<result>
<orderlist>
<order_no>123123</order_no>
<date></date>
<client_name>Knapp & Donut Shop</client_name>
<sector>54</sector>
</orderlist>
</result>
XML;
$xml = str_replace(array("&", "&"), array("&", "&"), $xml);
$xmlObj = simplexml_load_string($xml);
var_dump($xmlObj);
echo PHP_EOL . $xmlObj->orderlist->client_name;
// Result: Knapp & Donut Shop
Ive been trying every way possible to create cdata entries in my xml. My latest attempt is as follows. I can't even get passed for the first statement where im creating a new DOMDocument. Any ideas?
<?php
$xml = '
<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
<make name="Ford">
<model>Mustang</model>
</make>
<make name="Honda">
<model>Accord</model>
</make>
</cars>
';
$dom = new DOMDocument;
$dom->loadXML($xml);
$xml = simplexml_import_dom($dom);
print "working";
?>
You should not have any characters before the XML declaration. Remove the line break at $xml = '.
The neatest solution would be to use heredoc syntax:
$xml = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
<make name="Ford">
<model>Mustang</model>
</make>
<make name="Honda">
<model>Accord</model>
</make>
</cars>
XML;
Have a look at: DOMDocument::createCDATASection
$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
<make name="Ford">
<model>Mustang</model>
</make>
<make name="Honda">
<model>Accord</model>
</make>
</cars>
';
$dom = new DOMDocument;
$dom->loadXML($xml);
$cdataNode = $dom->createCDATASection('<&>');
$dom->documentElement->appendChild($cdataNode);
echo $dom->saveXml();
Output:
<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
<make name="Ford">
<model>Mustang</model>
</make>
<make name="Honda">
<model>Accord</model>
</make>
<![CDATA[<&>]]></cars>