create xml in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

create xml in php

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: create xml in php

Post by JAM »

What <xml encoding> tag are you using?

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Re: create xml in php

Post by bouncer »

JAM wrote:What <xml encoding> tag are you using?

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
that one. :roll:
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: create xml in php

Post 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).
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: create xml in php

Post 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
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Re: create xml in php

Post 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.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Re: create xml in php

Post by bouncer »

any ideas ? :(

regards
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: create xml in php

Post by JAM »

Not really just like that...
Perhaps applying utf8_encode() in any way could help?
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Re: create xml in php

Post 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. :wink:

regards
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: create xml in php

Post by Kieran Huggins »

Kieran Huggins wrote:does the problem persist if you use DOMDocument->createCDATASection() instead?
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Re: create xml in php

Post 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 :roll:

regards
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: create xml in php

Post 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".
Image

I had this problem with some xml documents, but doing so fixed it.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: create xml in php

Post by Kieran Huggins »

a CData section isn't a tag - it just escapes everything it surrounds.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Re: create xml in php

Post 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 - Importa‡”es e Exporta‡”es, Lda</CompanyName>
instead of
<CompanyName>Rasgo - Importações e Exportações, Lda</CompanyName>
regards
User avatar
hannnndy
Forum Contributor
Posts: 131
Joined: Sat Jan 12, 2008 2:09 am
Location: Iran>Tehran
Contact:

Re: create xml in php

Post 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
Post Reply