Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 958547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:53:08+00:00 2026-05-16T00:53:08+00:00

this code creates an xml file if it does not exist: $xmldoc = new

  • 0

this code creates an xml file if it does not exist:

$xmldoc = new DOMDocument();
    if(file_exists('test.xml')){
    $xmldoc->load('test.xml');
    } else {
    $xmldoc->loadXML('<root/>'); 
    }

however, i would also like encoding="UTF-8" to be appended automatically on creation of the file. how would one do this in php?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:53:09+00:00Added an answer on May 16, 2026 at 12:53 am

    To add/change the <?xml encoding value in the output of saveHTML(), you can set the encoding property.

    $xmldoc->load('test.xml');
    $xmldoc->encoding= 'utf-8';
    $xmldoc->save('test.xml');
    

    However, as Artefacto said, in this case it’s pointless. An XML file without an <?xml encoding declaration or a UTF-16 BOM is definitely UTF-8 and all XML parsers will read it that way(*). You will gain nothing by adding an explicit encoding="utf-8" parameter to the XML Declaration.

    Whatever the method is you’re using to test, it’s not doing what you think it’s doing. Maybe you’re loading XML into a text editor and it’s saving it out in a different encoding, or something? You need to look at where you’re getting the strings from that are going into the DOM before you save it, and if they’re not UTF-8 you need to convert them then.

    (*: Well unless it’s served via another protocol with a higher-priority charset specification method, like HTTP’s Content-Type header. But in that case the <?xml encoding declaration is ignored anyway.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm not sure if I fully understand what you are… May 16, 2026 at 11:59 am
  • Editorial Team
    Editorial Team added an answer You will need to name your MovieClips instances after the… May 16, 2026 at 11:59 am
  • Editorial Team
    Editorial Team added an answer Your version of Eclipse is Eclipse IDE for Java Developers.… May 16, 2026 at 11:59 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am creating an xml file. I need to check first if the file
Recently I found a piece of code that creates an instance of TButton from
I've got an XML file that I want to turn in to a list
I have an XSLT transform I am using to process an XML file, inserting
how do i check in xml [with php dom] that if a particular element
EDIT: See my working code in the answers below. In brief: I have a
I'm trying to write some code to automatically configure a bunch of scheduled tasks
I try many different solution on this site and none seems to work for
I have found some similar questions like this one, however there are so many
I am new to Android development since my EVO purchase a few months ago.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.