php xml output OK, but missing version header?

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

Post Reply
php_dadonred
Forum Newbie
Posts: 4
Joined: Sun Jun 13, 2010 12:00 am

php xml output OK, but missing version header?

Post by php_dadonred »

I'm using php 4.3.11 (no choice, I have to) and the dump_file method for writing an XML file. All is fine in terms of the tree, but I can't get the first line (xml version) to be output. :banghead:
It should be:
<?xml version="1.0"?>

I've tried:

Code: Select all

<?php
header('Content-Type: text/xml');
...
I also tried:

Code: Select all

<?php
$dom = domxml_new_doc('1.0');
$hdr = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>"; 
$topper = $dom->create_comment($hdr);
$topper = $dom->append_child($topper);
...
(which does generate a first line, but as a comment, of course).

And I tried

Code: Select all

<?php
$echo "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>";
...
I am missing something obvious? How do I get the first line of output in the *.xml file to be the version info??
The only I idea I have is to work with two files and fopen/fread/fwrite, but that seems a roundabout way...
Every example I see has the same approach, but shows the first line. (Note, I have to use dump_file or something similar.)


fyi, here is my generated xml file - I just need the first line to be added:

Code: Select all

<JSChart> 
  <dataset type="bar"> 
    <data unit="Jan" value="5"/> 
    <data unit="Feb" value="7"/> 
    <data unit="Mar" value="10"/> 
    <data unit="Apr" value="12"/> 
    <data unit="May" value="14"/> 
    <data unit="Jun" value="0"/> 
    <data unit="Jul" value="0"/> 
    <data unit="Aug" value="0"/> 
    <data unit="Sep" value="0"/> 
    <data unit="Oct" value="0"/> 
    <data unit="Nov" value="0"/> 
    <data unit="Dec" value="0"/> 
  </dataset> 
  <optionset> 
    <option set="setBackgroundColor" value="'#efe'"/> 
    <option set="setAxisNameX" value="'Month'"/> 
    <option set="setAxisNameY" value="'GB'"/> 
    <option set="setSize" value="320,256"/> 
    <option set="setTitle" value="'Historical Data Back-up Trend'"/> 
    <option set="setTitleColor" value="'#5555AA'"/> 
    <option set="setTitleFontSize" value="10"/> 
  </optionset> 
</JSChart> 
here is the first part and last part of the script:

Code: Select all

<?php
header('Content-Type: text/xml');

$dom = domxml_new_doc('1.0');
$root = $dom->create_element("JSChart");
$root = $dom->append_child($root);

$nxt_element = $dom->create_element("dataset");
$nxt_element = $root->append_child($nxt_element);
$type_attr = $dom->create_attribute("type","bar");
$nxt_element->append_child($type_attr);
...

$dom->dump_file("/tmp/test.xml", false, true);
?>
Last edited by Benjamin on Sun Jun 13, 2010 7:10 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php xml output OK, but missing version header?

Post by requinix »

Give create_processing_instruction a try.

Code: Select all

$dom->create_processing_instruction("xml", "version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"");
php_dadonred
Forum Newbie
Posts: 4
Joined: Sun Jun 13, 2010 12:00 am

Re: php xml output OK, but missing version header?

Post by php_dadonred »

Can you help me understand if I'm using this correctly? Here is what I inserted:

$dom = domxml_new_doc('1.0');
$hdr = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>";
$topper = $dom->create_processing_instruction($hdr);
$topper = $dom->append_child($topper);

but it didn't insert anything into the xml doc.
php_dadonred
Forum Newbie
Posts: 4
Joined: Sun Jun 13, 2010 12:00 am

Re: php xml output OK, but missing version header?

Post by php_dadonred »

I tried the following, but no modification to the output document:

$dom = domxml_new_doc('1.0');
$topper = $dom->create_processing_instruction("xml", "version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"")
$topper = $dom->append_child($topper);
php_dadonred
Forum Newbie
Posts: 4
Joined: Sun Jun 13, 2010 12:00 am

Re: php xml output OK, but missing version header?

Post by php_dadonred »

:oops:
I thought about it and downloaded the *.xml file that was created to my machine - sure enough the xml declaration is there. For some reason it doesn't appear when I opened that file in my browser. Now to figure out if it was there all along. Thanks for your assistance, though.
rd.dcse
Forum Newbie
Posts: 7
Joined: Fri Jun 11, 2010 2:56 pm

Re: php xml output OK, but missing version header?

Post by rd.dcse »

Hi, I am new to XML.

I want ta ask a xml question. I am using PHP. Having a 3rd party xml query and when i fire it in there website it revert with a code i want to accept this code.

Sample codes are given below.


Input
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE REQUEST SYSTEM "http://api.website.com/psms/dtd/request.dtd" >
<REQUEST USERNAME="demo" PASSWORD="demo">
</REQUEST>



Output
<Credit User="demo">
<Credit Limit="100" Used="1.00"/>
</Credit>


Sorry but i have posted it so many times and didnt get any reply so i asked you.

Thanks in advance
Post Reply