write xml file

to write a the new dom to a file use:

http://javaalmanac.com/egs/javax.xml.transform/WriteDom.html

but how can I write file with encoding such as "UTF-8"?
keepworkingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
it'll be utf8 by default i think
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
keepworkingAuthor Commented:
thanks objects:

when I use the following method to create and write data to xml file, it works for English data, but when I use it to write data in German or Ascian language, it doesn't work, error:java.io.UTFDataFormatException: Invalid byte 1 of 1-byte UTF-8 sequence.

----------------------------------------------------------------------------------------------
to create a new dom

http://javaalmanac.com/egs/javax.xml.parsers/CreateDom.html?l=rel

to add nodes to new doc

http://javaalmanac.com/egs/org.w3c.dom/AddNode.html

to write a the new dom to a file use:

http://javaalmanac.com/egs/javax.xml.transform/WriteDom.html
--------------------------------------------------------------------------------------------------------
0
objectsCommented:
try:

Properties props = new Properties();
props.setProperty("encoding", "UTF-8");
xformer.setOutputProperties(props);
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

keepworkingAuthor Commented:
import java.util.Properties;

I did, same error.

thanks
0
Manikandan ThiagarajanSenior consultantCommented:
Properties props = new Properties();
props.setProperty("characterEncoding", "utf-8");

try this one
0
borislavmarkovCommented:
I suggest to add your strings to a Vector and see how they will be serialized with java XML serializator.
With JDK1.4 you can do:

       XMLEncoder enc = new XMLEncoder(new FileOutputStream("d:\\out.xml"));

        Vector v= new Vector();
        v.add("\u00e4");
        v.add("\u0333");
        enc.writeObject(v);
        enc.flush();
        System.out.println("v = " + v);

Then take the result and see it.
0
CEHJCommented:
Try


Result result = new StreamResult(new OutputStreamWriter(new FileOutputStream("x.xml"), "UTF-8"));
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.