473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Unicode problem with Java Xerces DOM

I'm having trouble with Unicode encoding in DOM. As a simple example,
I read in a UTF-8 encoded xml file such as:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<aText>letter 'a' with umlaut: ä</aText>

And when I serialize it, it comes out encoded as ISO-8895-1. But I
don't think the problem is with serialization. In processing my XML
files, I'm matching bits and pieces of text and attributes with some
Unicode/UTF-8 text read in from another souce. When the strings in my
XML file contain non-ASCII characters, then I have problems.

Hopefully, I've explained the problem enough so that someone can help.
In case it's necessary, I attach at the end, a bit of code for reading
in and serializing a DOM.

Dale Gerdemann
----------------
import org.xml.sax.InputSource;
import java.io.FileInputStream;
import java.io.File;
import java.io.FileWriter;
import org.w3c.dom.Document;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.dom.DOMImplementationImpl;
import org.xml.sax.SAXException;
import org.w3c.dom.DOMException;
import java.io.IOException;
import org.w3c.dom.Element;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.LineSeparator;
public class AProblem {

public static void main(String[] args)
throws DOMException, IOException, SAXException {

DOMParser parser = new DOMParser();
InputSource is = new InputSource(new FileInputStream(new
File("foo.xml")));
is.setEncoding("UTF-8");
parser.parse(is);
Document doc = parser.getDocument();
Element root = doc.getDocumentElement();
System.out.println(root.getChildNodes().item(0));

OutputFormat format = new OutputFormat(doc);
format.setLineSeparator(LineSeparator.Unix);

format.setIndenting(true);
format.setLineWidth(0);
format.setPreserveSpace(true);
format.setEncoding("UTF-8");
FileWriter fw = new FileWriter("bar.xml");

XMLSerializer serializer = new XMLSerializer(fw, format);
serializer.serialize(doc);
}
}
Jul 20 '05 #1
2 9900
Dale Gerdemann wrote:
I'm having trouble with Unicode encoding in DOM. As a simple example,
I read in a UTF-8 encoded xml file such as:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<aText>letter 'a' with umlaut: ä</aText>

And when I serialize it, it comes out encoded as ISO-8895-1. But I
don't think the problem is with serialization. In processing my XML
files, I'm matching bits and pieces of text and attributes with some
Unicode/UTF-8 text read in from another souce. When the strings in my
XML file contain non-ASCII characters, then I have problems.

Dale,

What JDK are you using and under which env?

Regards,
Kenneth
Jul 20 '05 #2
In article <93**************************@posting.google.com >,
dg@sfs.nphil.uni-tuebingen.de (Dale Gerdemann) wrote:
:I'm having trouble with Unicode encoding in DOM. As a simple example,
:I read in a UTF-8 encoded xml file such as:
:
:<?xml version="1.0" encoding="UTF-8" standalone="no"?>
:
:<aText>letter 'a' with umlaut: ä</aText>
:
:And when I serialize it, it comes out encoded as ISO-8895-1. But I
:don't think the problem is with serialization. In processing my XML
:files, I'm matching bits and pieces of text and attributes with some
:Unicode/UTF-8 text read in from another souce. When the strings in my
:XML file contain non-ASCII characters, then I have problems.
:
:Hopefully, I've explained the problem enough so that someone can help.
:In case it's necessary, I attach at the end, a bit of code for reading
:in and serializing a DOM.
:
:Dale Gerdemann
:----------------
:import org.xml.sax.InputSource;
:import java.io.FileInputStream;
:import java.io.File;
:import java.io.FileWriter;
:import org.w3c.dom.Document;
:import org.apache.xerces.parsers.DOMParser;
:import org.apache.xerces.dom.DOMImplementationImpl;
:import org.xml.sax.SAXException;
:import org.w3c.dom.DOMException;
:import java.io.IOException;
:import org.w3c.dom.Element;
:import org.apache.xml.serialize.OutputFormat;
:import org.apache.xml.serialize.XMLSerializer;
:import org.apache.xml.serialize.LineSeparator;
:
:
:public class AProblem {
:
: public static void main(String[] args)
: throws DOMException, IOException, SAXException {
:
: DOMParser parser = new DOMParser();
: InputSource is = new InputSource(new FileInputStream(new
:File("foo.xml")));
: is.setEncoding("UTF-8");
: parser.parse(is);
: Document doc = parser.getDocument();
: Element root = doc.getDocumentElement();
: System.out.println(root.getChildNodes().item(0));
:
:
:
: OutputFormat format = new OutputFormat(doc);
: format.setLineSeparator(LineSeparator.Unix);
:
: format.setIndenting(true);
: format.setLineWidth(0);
: format.setPreserveSpace(true);
: format.setEncoding("UTF-8");
: FileWriter fw = new FileWriter("bar.xml");
:
: XMLSerializer serializer = new XMLSerializer(fw, format);
: serializer.serialize(doc);
:
:
: }
:}


I've encountered this problem myself. The solution was to use something
besides a FileWriter to output your new XML document, since you need to
encode both the XML data and the data written to an external file.

Your OutputFormat object specifies that the XML gets UTF-8 encoding, but
the FileWriter will use your system's default encoding. What I use now
is an OutputStreamWriter with its constructor taking an OutputStream (I
use a FileOutputStream) and a String naming the encoding. That solved
the problem for me.

I also note that you're specifying UTF-8 on input. While I doubt it
does any harm, it shouldn't be necessary.

Hope this helps.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael | last post by:
Hello I am trying to write a Java-Program which converts a XML-file in a HTML. It should take the Transformation-file from the XML-file itself. Below find a possible XML-file: <?xml...
0
by: peter greaves | last post by:
hi everyone i am having a bad time with an entity resolver. my application uses a resolver to locally-cache the nested schemas that the basic xsd includes to a local directory. however i am...
3
by: Bobo | last post by:
I am getting the following error while trying to process an XML string with unicode in its content. :12:0: An invalid XML character (Unicode: 0x0) was found in the eement content of the...
0
by: Dale Gerdemann | last post by:
I've been trying to use DOM level 3 with xerces-2_6_2. There's a sample called samples/DOM3.java, but I've had trouble with compilation. I've downloaded Xerces-J-bin.2.6.2 and...
4
by: SL | last post by:
Hi, Im' using Xerces-j (version 2.0.1 and 2.6.2). When parsing this prolog : <!DOCTYPE teiCorpus PUBLIC "-//TEI Consortium//DTD TEI P4//EN" "d:/Program Files/tei-emacs/sgml/dtds/tei/tei2.dtd"...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
1
by: Jens Mueller | last post by:
Hi there, this is a Java-XML Question, so I am not sure whether this is the right place, haven't found anything better .... I try to convert a Java object to XML via SAX and let the FOP...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
1
by: Simon Brooke | last post by:
Yet another silly question, but this just might be the crucial one. In answer to another of my silly questions, Björn Höhrmann pointed me to http://home.ccil.org/~cowan/XML/tagsoup/, and as I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.