I'm trying to create an XML from PHP with special characters.
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ' . 'standalone="yes"?><Root/>');
$data->addChild('NAME', $variable);
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xmlAusgabe = $xml->asXML());
$dom->save('../test.xml');
When there is a special character like '&' in it the output is empty.
I thought these characters are available in UTF-8.
Can someone help me?
When embed XML, always there are many problems :( try this:
Change
$dom->loadXML($xmlAusgabe = $xml->asXML());
by
$xmlAusgabe = $xml->asXML();
$xmlAusgabe = mb_convert_encoding( $xmlAusgabe, 'HTML-ENTITIES', 'UTF-8') ;
$dom->loadXML( $xmlAusgabe );
Then check encoding and fileencoding is utf8. If you use vim editor:
set encoding=utf-8
set fileencoding=utf-8
UPDATE
libxml_use_internal_errors( FALSE );
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Root/>');
$data->addChild('NAME', mb_convert_encoding( $variable, 'HTML-ENTITIES', 'UTF-8') );
$xmlAusgabe = $xml->asXML();
$dom = new DOMDocument();
$dom->loadXML( $xmlAusgabe );
$dom->save('../test.xml');
I check this code and run. To print error use:
libxml_get_errors()
Related
I need to create an XML in PHP.
My code start with:
$doc = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><iv:Fornitura xmlns:cm="urn:www.agenziaentrate.gov.it:specificheTecniche:common" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:tm="urn:www.agenziaentrate.gov.it:specificheTecniche:telent:v1" xmlns:iv="urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp"></iv:Fornitura>', LIBXML_NOERROR, false, 'iv', true);
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom_xml = dom_import_simplexml($doc);
$dom_xml = $dom->importNode($dom_xml, true);
$dom_xml = $dom->appendChild($dom_xml);
$file_XML = $dom->saveXML(); // SAVE TO STRING
Expected result:
<?xml version="1.0" encoding="UTF-8"?>
<iv:Fornitura xmlns:cm="urn:www.agenziaentrate.gov.it:specificheTecniche:common" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:iv="urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp">
</iv:Fornitura>
But I always get:
<?xml version="1.0"?>
<iv:Fornitura xmlns:cm="urn:www.agenziaentrate.gov.it:specificheTecniche:common" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:iv="urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp">
</iv:Fornitura>
It seem encoding encoding="UTF-8" disappear. Using also addAttribute doesn't work.
Rather than disappearing, I'd say it never gets added in the first place:
$dom = new DOMDocument('1.0');
As per docs:
public DOMDocument::__construct ([ string $version [, string $encoding ]] )`
Demo
i have a xml file, when i have load this file using DOMDocument:: load and save it gives white space, please suggest how to remove this white space. Below is my code.
$file = 'myfile.xml'
$doc = new DOMDocument('1.0', 'utf-8');
$doc->preserveWhiteSpace = false;
$doc->load($file);
echo htmlentities($doc->saveXML());exit;
It give output like this
<?xml version="1.0"?> <Esign ver="2.0"></Esign>
how to remove this space
Actually i am trying to perform digital signature
below is my function for digital signature, In this function i found that space added after $doc->load($file)
public function digital_sign($file){
$doc = new DOMDocument('1.0', 'utf-8');
$doc->preserveWhiteSpace = false;
$doc->load($file);
// Create a new Security object
$objDSig = new XMLSecurityDSig();
// Use the c14n exclusive canonicalization
$objDSig->setCanonicalMethod(XMLSecurityDSig::C14N_COMMENTS);
// Sign using SHA-256
$objDSig->addReference($doc,XMLSecurityDSig::SHA256,array('http://www.w3.org/2000/09/xmldsig#enveloped-signature'),array('force_uri'=>'true'));
// Create a new (private) Security key
$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, array('type'=>'private'));
// Load the private key
$objKey->loadKey('private_key.pem', TRUE);
$objDSig->sign($objKey);
// Add the associated public key to the signature
$options['subjectName'] = "true";
$objDSig->add509Cert(file_get_contents('public_key.cer'),true,false,$options);
// Append the signature to the XML
$objDSig->appendSignature($doc->documentElement);
$filenewpath = 'newfile1.xml';
ob_clean();
if($doc->save($filenewpath)){
return true;
}
}
$file contain below xml
<Esign ver="2.0" sc="Y" ts="2018-03-30T07:10:47" txn="97051198" ekycMode="U" ekycId="88XXXXXXXXX" ekycIdType="A" aspId="ASPSXXXXXXXXXXX156" AuthMode="1" responseSigType="pkcs7" preVerified="n" organizationFlag="n" responseUrl="https://www.google.com"><Docs><InputHash id="1" hashAlgorithm="SHA256" docInfo="Test pdf">60bdd13ea4827b8de375c79dc3ff847f83b55bd73b6461523fdf8f843b5a0d5b</InputHash></Docs></Esign>
Output After signature
<?xml version="1.0"?> <Esign ver="2.0" sc="Y" ts="2018-03-30T07:21:03" txn="39242002" ekycMode="U" ekycId="88XXXXXXXXX" ekycIdType="A" aspId="ASPSXXXXXXXXXXX156" AuthMode="1" responseSigType="pkcs7" preVerified="n" organizationFlag="n" responseUrl="https://www.stage.kissht.com/welcome/email"><Docs><InputHash id="1" hashAlgorithm="SHA256" docInfo="Test pdf">60bdd13ea4827b8de375c79dc3ff847f83b55bd73b6461523fdf8f843b5a0d5b</InputHash></Docs><Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"/><SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/></Transforms><DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><DigestValue>bstOvGSSvkuEcriLXk6Qj2nhzEb3BZ1oEUtOYoMFyCw=</DigestValue></Reference></SignedInfo><SignatureValue>Uj3aSvmbMhUJX5Bwj3Bk3/F5U6IFx4LMS1BWMLTeEtnMvcfrJo25YGZHQtt1CnPbT5ygKEU7dQ3IBRsghqDvG+VAHYbU9gfX1N/5NxLM15oGWhZ3oSlNv7ouS1Ajz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M27zlyYqQHXw867koX3CNNw4onRPSoxFEv1yThaYSA5MUFp5OQlq7/wKWWhKh842BVRJ7ov7iLSUXHSEmvbw70czribN3Ps9xXnOeHT+REchBeshttjdaZ7NTMyITVPcL6rzZ3Z8FT1ce9vtl4akls9ghXRRBTatzP/QKztLcxyB7RkGXV+tESeAhPqnP9RsHbGLRCQdIo9yEc4nJxDvuItxdTs/BcUTLpxeYaUlYYhXAeMWmHMsm5EtGH6eTyG3kWyaFyQF2aMdwGALxaHHie/OtLWm5w1tecOfpymO6uhlC6dfzhQTBwgHJGw/8P+wSK7gQnQbfG3zSQiGJn2vjy9sHTbmJoLcOc5ZhyhaWSMymTepXKwXpI</X509Certificate></X509Data></KeyInfo></Signature></Esign>
how to remove this space
<?xml version="1.0"?> <Esign
The saveXML() function creates a line-break after the <?xml ?> tag. Displayed in HTML environment, you see a "space".
You can use str_replace() to remove this "\n" before to use htmlentities() for this specific case:
$file = 'myfile.xml';
$doc = new DOMDocument('1.0', 'utf-8');
$doc->preserveWhiteSpace = false;
$doc->load($file);
$xml = $doc->saveXML();
$xml = str_replace("?>\n<", '?><', $xml);
// $xml = str_replace("?>".PHP_EOL."<", '?><', $xml);
// $xml = preg_replace("~\?>\s+<~", '?><', $xml);
echo htmlentities($xml);
exit;
Will outputs:
<?xml version="1.0"?><Esign ver="2.0"></Esign>
I have a question: I am trying to create a XML file using DomDocument and I would like to have this output:
<?xml version="1.0" encoding="UTF-8"?>
<winstrom version="1.0">
<main_tag>
<child_tag>example</child_tag>
</main_tag>
<winstrom>
The problem is with the second row - if I write it as below then the output is "Invalid Character Error". I guess it is not allowed to have space characters there... However I need it like this, so what are the options?
$dom = new DomDocument('1.0', 'UTF-8');
$root = $dom->createElement('winstrom version=1.0');
$dom->appendChild($root);
$item = $dom->createElement('hlavni_tag');
$root2->appendChild($item);
$text = $dom->createTextNode('example');
$item->appendChild($text);
$dom->formatOutput = true;
echo $dom->saveXML();
There seems to be a misunderstanding of what an XML element is and how it differs from attributes.
Try this code:
<?php
$dom = new DomDocument('1.0', 'UTF-8');
$root = $dom->createElement('winstrom');
$root->setAttribute("version","1.0");
$dom->appendChild($root);
$root2 = $dom->createElement("main_tag"); //You forgot this part
$root->appendChild($root2);
$item = $dom->createElement('hlavni_tag'); //Should it be "child_tag"?
$root2->appendChild($item);
$text = $dom->createTextNode('example');
$item->appendChild($text);
$dom->formatOutput = true;
echo $dom->saveXML();
I have the following code in my PHP script:
$str = '<item></item>';
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$xml->load('file.xml');
$items = $addon->getElementsByTagName('items')->item(0);
$items->nodeValue = $str;
$xml->save('file.xml');
In the saved file.xml I see the following:
<item><\item>
How can I save it in the XML file without encoding HTML entities?
Use a DOMDocumentFragment:
<?php
$doc = new DOMDocument();
$doc->load('file.xml'); // '<doc><items/></doc>'
$item = $doc->createDocumentFragment();
$item->appendXML('<item id="1">item</item>');
$doc->getElementsByTagName('items')->item(0)->appendChild($item);
$doc->save('file.xml');
If you're appending to the root element, use $doc->documentElement->appendChild($item); instead of getElementsByTagName.
I cannot figure out how to stop DOMDocument from mangling these characters.
<?php
$doc = new DOMDocument();
$doc->substituteEntities = false;
$doc->loadHTML('<p>¯\(°_o)/¯</p>');
print_r($doc->saveHTML());
?>
Expected Output:
¯(°_o)/¯
Actual Output:
¯(°_o)/¯
http://codepad.org/W83eHSsT
I've found a hint in the comments of DOMDocument::loadHTML documentation:
(Comment from <mdmitry at gmail dot com> 21-Dec-2009 05:02: "You can also load HTML as UTF-8 using this simple hack:")
Just add '<?xml encoding="UTF-8">' before the HTML-input:
$doc = new DOMDocument();
//$doc->substituteEntities = false;
$doc->loadHTML('<?xml encoding="UTF-8">' . '<p>¯\(°_o)/¯</p>');
print_r($doc->saveHTML());
<?xml version="1.0" encoding="utf-8">
in the top of the document takes care of tags.. for both saveXML and saveHTML.