Page 1 of 2
create xml in php
Posted: Thu Jan 24, 2008 11:39 am
by bouncer
hi there, i'm having some issues while generating a xml file with php
Code: Select all
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$City = $doc->createElement ( "City" );
$City->appendChild( $doc->createTextNode( $row['CLOC'] ) ); <----- issue
$BillingAddress->appendChild( $City );
if i have a string like this "asdadçâsadaãoç" in $row['CLOC'], and then open the .xml file it gives me a message saying that i have a invalid caractere, can anyone tell me how to avoid this issue ?
thanks in advance
Re: create xml in php
Posted: Thu Jan 24, 2008 11:45 am
by JAM
What <xml encoding> tag are you using?
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
Re: create xml in php
Posted: Thu Jan 24, 2008 11:46 am
by bouncer
JAM wrote:What <xml encoding> tag are you using?
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
that one.

Re: create xml in php
Posted: Thu Jan 24, 2008 12:09 pm
by JAM
Tried this one?
Code: Select all
<?xml version="1.0" encoding="windows-1252"?>
Also, stick with the utf one, but check so that your editor is saving it correct. I use UltraEdit myself and that has quite some settings for dealing with UTF saving (with or without BOM, UTF-8 or 16 aso aso).
Re: create xml in php
Posted: Thu Jan 24, 2008 12:36 pm
by Kieran Huggins
does the problem persist if you use DOMDocument->createCDATASection() instead?
also, you could try setting the charset with a header() as well
Re: create xml in php
Posted: Fri Jan 25, 2008 9:01 am
by bouncer
i've changed from $doc = new DOMDocument('1.0', 'utf-8'); to $doc = new DOMDocument('1.0', 'windows-1252'); and $doc = new DOMDocument('1.0', 'ISO-8859-1'); without solving this issue. i've also used header("Content-type: text/xml; charset=utf-8");
any ideas ?
one more thing how can i add this xml namespace (xmlns) attribute to a element ( xmlns:namespace-prefix="namespaceURI" ) ?
thanks in advance.
Re: create xml in php
Posted: Sat Jan 26, 2008 5:29 pm
by bouncer
any ideas ?
regards
Re: create xml in php
Posted: Sat Jan 26, 2008 5:40 pm
by JAM
Not really just like that...
Perhaps applying
utf8_encode() in any way could help?
Re: create xml in php
Posted: Sat Jan 26, 2008 5:49 pm
by bouncer
i've tried that function but instead of "ã" i get a empty square, maybe because of DOM ?
can you tell me if there is a better way to create a xml file ?
i've found this function htmlentities(), will test this as soon as possible and reply telling if it work.
regards
Re: create xml in php
Posted: Sun Jan 27, 2008 1:24 am
by Kieran Huggins
Kieran Huggins wrote:does the problem persist if you use DOMDocument->createCDATASection() instead?
Re: create xml in php
Posted: Sun Jan 27, 2008 1:26 pm
by bouncer
no it doesn't work, with that it says that i need to close the tags.
and that will create a tag like this <![CDATA[something]]> and i need a normal tag like this one <employeename>name</employeename>
sorry i'm new with xml
regards
Re: create xml in php
Posted: Mon Jan 28, 2008 4:07 am
by Sindarin
Try opening the xml file in notepad, save as. In the save dialog select Save as type: "All files" and Encoding: "UTF-8".
I had this problem with some xml documents, but doing so fixed it.
Re: create xml in php
Posted: Mon Jan 28, 2008 2:38 pm
by Kieran Huggins
a CData section isn't a tag - it just escapes everything it surrounds.
Re: create xml in php
Posted: Tue Jan 29, 2008 4:06 am
by bouncer
i've tried with CData section, and that didn´t help, i also use utf8_encode with no errors but it give me this:
<CompanyName>Rasgo - Importaes e Exportaes, Lda</CompanyName>
instead of
<CompanyName>Rasgo - Importações e Exportações, Lda</CompanyName>
regards
Re: create xml in php
Posted: Tue Jan 29, 2008 4:47 am
by hannnndy
i have such problem too as the friends say i
1.open the xml file with notepad in windows and save as utf-8
2.
Code: Select all
//Creates XML string and XML document using the DOM
$dom = new DomDocument('1.0');
//add root - <links>
$links = $dom->appendChild($dom->createElement('links'));
//add Dummy Node
$linkNode = $links->appendChild($dom->createElement('link'));
$linkNode->appendChild($dom->createTextNode('Dummy'));
$linkNode->setAttribute("url", "www.parsiTech.com");
//generate xml set the format Output attribute of domDocument to true
$dom->formatOutput = true;
// save XML as string or file put string in data
$data = $dom->saveXML();
$dom->save($file); // save as file
i have used this for Arabic,Russian,Persian characters which are Unicode
and one thing you should not forget is it is not important what the editors show you after creating the file it is important that when you open the spesified xml file through the php code it would work properly