This question already has an answer here:
Create XML with xmlns:xlink attribute in a node
(1 answer)
Closed 8 years ago.
Thanks for your help, I need php script to generate the following XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<design xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://anydomain.com">
<name>xxx</name>
<description>yyy</description>
</design>
You could use SimpleXML to create such xml.
Rough example:
$xml = new SimpleXMLElement('<design />'); // set parent node
$xml->addAttribute('xmlns', 'http://anydomain.com'); // attributes
$xml->addAttribute('xlink:ns', '', 'http://www.w3.org/1999/xlink');
unset($xml->attributes('xlink', true)['ns']);
$xml->addChild('name', 'xxx'); // add those children
$xml->addChild('description', 'yyy');
echo $xml->asXML(); // output
Related
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
My asmx WEB service return this XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<PRODUCT>
<DESC>Vanilla ice cream</DESC>
<CODEERR>0</CODEERR>
</PRODUCT>
Calling WEB service from this PHP code
$SoapCli = new SoapClient('http://www.foo.com/MyService.asmx?WSDL');
$params = array(
'PARAM1' => 'some_param_1',
'PARAM2' => 'some_param_2',
);
$resp_WS = $SoapCli->__soapCall('MyFunction', array($params));
var_dump($resp_WS);
result is
object(stdClass)#11946 (1) {
["MyFunctionResult"]=>
object(stdClass)#11947 (1) {
["any"]=>
string(88) "<product xmlns=""><desc>Vanilla ice cream</desc><codeerr>0</codeerr></product>"
}
}
but, after googling a lot, I don't find PHP code for retreive values of two fields DESC and CODER
You can use json_encode,json_decode,simplexml_load_string to parse the XML response, try the following code snippet to read the XML response
$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
<PRODUCT>
<DESC>Vanilla ice cream</DESC>
<CODEERR>0</CODEERR>
</PRODUCT>';
$res = json_decode(json_encode((array)simplexml_load_string($xml)),true);
Now you can use $res['DESC'] and $res['CODEERR'] to retrive the values.
This question already has answers here:
How to convert XML attributes to text nodes
(3 answers)
Closed 7 years ago.
I want to write a PHP script that will modify my XML file.
I have my productId within the node as an attribute and I want to parse the entire file and convert it to a separate node. So I want to read the attribute of the node and put that attribute in its own node. But the rest of the nodes will stay as is.
Before:
<product id="123">
<name>bob</name>
<lastname>tim</lastname>
</product>
To:
<product>
<id>123</id>
<name>bob</name>
<lastname>tim</lastname>
</product>
Can I do this in PHP? Bearing in mind the file will have over one thousand separate products in it.
You could do it this way.
$xml = new SimpleXMLElement('<product id="123"></product>');
if(!empty($xml['id'])) {
$xml->addChild('id', $xml['id']);
unset($xml['id']);
}
echo $xml->asXML();
Output:
<?xml version="1.0"?>
<product><id>123</id></product>
Here's the manual's link and the addchild functions link. http://php.net/manual/en/class.simplexmlelement.php
http://php.net/manual/en/simplexmlelement.addchild.php
Update:
If you had multiple products you could loop like this.
$xml = new SimpleXMLElement('<proudcts><product id="123"></product><product id="234"></product></proudcts>');
foreach($xml as $key => $data){
if(!empty($data['id'])) {
$data->addChild('id', $data['id']);
unset($data['id']);
}
}
echo $xml->asXML();
Output:
<?xml version="1.0"?>
<proudcts><product><id>123</id></product><product><id>234</id></product></proudcts>
This question already has answers here:
SimpleXML SOAP response Namespace issues
(2 answers)
Closed 8 years ago.
I found lot of solutions for this problem, but my code won't work!
XML INFO:
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:resultado xmlns:ns2="http://webservice.consulta.spcjava.spcbrasil.org/" data="2014-06-03T11:37:32.001-03:00" restricao="false">
<protocolo digito="2" numero="1204248496" />
.... other XML info
MY CODE:
$s = '<?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:resultado xmlns:ns2="http://webservice.consulta.spcjava.spcbrasil.org/" data="2014-06-03T11:37:32.001-03:00" restricao="false"><protocolo digito="2" numero="1204248496" /> ...
$xml = simplexml_load_string($s);
$x2 = $xml->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://webservice.consulta.spcjava.spcbrasil.org/')->resultado->protocolo->digito;
print_r($x2);
var_dump(count($x2));
Returns null and 0 for the object count.
I've been following this tutorial: http://amigotechnotes.wordpress.com/2013/11/16/parse-xml-with-namespace-by-simplexml-in-php/
I don't get where my example differs from his example. :/
Can anyone help me with this issue, please?
Finally made it work using another syntax.
$xml = simplexml_load_string($resultadoDocumento[0]["cdo_xml"]);
$resultadoSPC = $xml->children('S', TRUE)->Body->children('ns2', TRUE)->children();
accessing nodes by:
<?php foreach ($resultadoSPC->consumidor->children()->{"consumidor-pessoa-fisica"} as $consumidorElement) : ?>
<?php echo $consumidorElement->attributes()->{"nome"}; ?>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Getting SimpleXMLElement to include the encoding in output
PHP :
$xml = new SimpleXMLElement("<Title0></Title0>");
$xml->Title1='Some Text 1';
$output = $xml->asXML('mak.xml');
XML Output :
<?xml version="1.0"?>
<Title0>
<Title1>Some Text 1</Title1>
</Title0>
But I want to add attrbute like encoding="utf-8" to xml header , so that I can have something like this :
<?xml version="1.0" encoding="utf-8" ?>
<Title0>
<Title1>Some Text 1</Title1>
</Title0>
I dont want to use find and replace sort of things on the output file.
Read this:--
http://php.net/manual/en/simplexml.examples-basic.php
try this:-
Example #10 Adding elements and attributes
<?php
include 'example.php';
$movies = new SimpleXMLElement($xmlstr);
$character = $movies->movie[0]->characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');
$rating = $movies->movie[0]->addChild('rating', 'PG');
$rating->addAttribute('type', 'mpaa');
echo $movies->asXML();
?>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP XML how to output nice format
I have the following PHP DOM code that creates a XML document in a dynamic way:
<?php
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
for ($i=1;$i<=5;$i++)
{
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
}
// save and display tree
echo $dom->saveXML();
?>
The PHP code generates the next XML code:
<?xml version="1.0"?>
<toppings><item>pepperoni</item></toppings>
<toppings><item>pepperoni</item></toppings>
<toppings><item>pepperoni</item></toppings>
<toppings><item>pepperoni</item></toppings>
<toppings><item>pepperoni</item></toppings>
I would like to know what to change in order for the XML look like this other way:
<?xml version="1.0"?>
<toppings>
<item>pepperoni</item>
</toppings>
<toppings>
<item>pepperoni</item>
</toppings>
<toppings>
<item>pepperoni</item>
</toppings>
<toppings>
<item>pepperoni</item>
</toppings>
<toppings>
<item>pepperoni</item>
</toppings>
Try $dom->formatOutput = true.