How to add attribute to xml header using SimpleXMLElement [duplicate] - php

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();
?>

Related

PHP XML - Convert XML node attribute to just a node [duplicate]

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>

PHP Script to generate XML with namespaces and attributes [duplicate]

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

Parsing XML with namespace information [duplicate]

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"}; ?>

XML file creation using PHP [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
XML formatting is not working well
I am trying to create an XML file from the database. Database contains name, phone no and sex. I would like to get all the users' details in a well-formatted XML file. But I am getting now:
<CUSTOMERS>
<name>AAA</name>
<name>BBB</name>
</CUSTOMERS>
This is my code:
$xmlstr = "<?xml version='1.0' ?>\n"."<CUSTOMERS></CUSTOMERS>";
$xml = new SimpleXMLElement($xmlstr);
while($b=$result->fetch_assoc()){
$xml->addChild("name", $b['name']);
}
return $xml->asXML();
I would like to get the out put as shows below
<CUSTOMERS>
<AAAA>
<name>AAA</name>
<phone>111</phone>
<sex>male</sex>
</AAA>
<BBBB>
<name>BBB</name>
<phone>222</phone>
<sex>female</sex>
</AAA>
</CUSTOMERS>
Latest Code
$xmlstr = "<?xml version='1.0' ?>\n"."<CUSTOMERS></CUSTOMERS>";
$xml = new SimpleXMLElement($xmlstr);
while($b=$result->fetch_assoc()){
$customer = $xml->addChild("customer");
$customer->addChild("name", $b['name']);
$customer->addChild("phone", $b['phone']);
$customer->addChild("sex", $b['sex']);
//$xml->addChild("place", $b['place']);
}
return $xml->asXML();
Each customer should be in its own tag:
$d = simplexml_load_string('<?xml version="1.0" encoding="utf-8" ?><customers />');
$customer = $d->addChild('customer');
$customer->addChild('name', 'Jack');
// $customer->addChild('phone', '911');
// etc.
$customer = $d->addChild('customer');
$customer->addChild('name', 'John');
echo $d->asXML();
<?xml version="1.0" encoding="utf-8"?>
<customers>
<customer><name>Jack</name></customer>
<customer><name>John</name></customer>
</customers>
Note that the tag name should not be the name of your customer, it should rather be a description of what information is inside; hence my use of the customer tag.
You should be able to do it like this:
$xml = new SimpleXMLElement("<customers></customers>");
while($b=$result->fetch_assoc()){
$customer = $xml->addChild("customer");
$customer->addChild("name", $b['name']);
$customer->addChild("phone", $b['phone']);
$customer->addChild("sex", $b['sex']);
}
Output:
<customers>
<customer>
<name>AAA</name>
<phone>1234567</phone>
<sex>f</sex>
</customer>
</customers>
You are telling PHP to add the <name> child but nothing else.
$xml->addChild("name", $b['name']);
In that while loop, you need to add the <AAA> element, then the child <name>, <phone>, and <sex> (and etc) elements.
The question is to change this
$xmlstr = "<?xml version='1.0' ?>\n"."<CUSTOMERS></CUSTOMERS>";
to this
$xmlstr = = '<' . '?xml version='1.0' ?>\n"."<CUSTOMERS></CUSTOMERS>';
As the <? messes up the parser.
Also XML does not like capitals and needs a root - i.e. something between your customers.

PHP DOM generated XML break line [duplicate]

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.

Categories