You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Subhasis Sanyal <Su...@catsglobal.co.in> on 2003/02/07 14:37:12 UTC
Kindly Solve this UTF-16 problem
> Hi,
> We have been trying hard to create a UTF-16 xml document. We have
> tried the following code:
> As you can see, we have tried to set a feature to allow java-encoding, but
> it is not working. The command line argument to the
> program is a xml filename.We are creating a Unicode format xml, with the
> encoding header set to UTF-16, and sending it to the
> program. We are using the docToStr() function function to print the
> convert the document object to a string, and display it on the
> command prompt. However, the printed output shows the encoding to be:
> UTF-8.
>
> Can you help on this issue? Is it a problem in the DOMParser itself, or
> the Transform API (in the docToStr() function is printing this
> wrong?) We have also tried the
> org.apache.xerces.jaxp.DocumentBuilderFactoryImpl class for XML parsing,
> and that is also giving us the same result.
>
> **************************************************************************
> *******************************************************************
>
> import javax.xml.parsers.DocumentBuilder;
> import javax.xml.parsers.DocumentBuilderFactory;
> import org.w3c.dom.Document;
> import org.apache.xerces.parsers.DOMParser;
> import javax.xml.transform.Transformer;
> import javax.xml.transform.TransformerFactory;
> import javax.xml.transform.dom.DOMSource;
> import javax.xml.transform.stream.StreamResult;
> import java.io.StringWriter;
>
> public class SchemaTest2
> {
> public static void main(String[] args)
> {
> try
> {
> DOMParser p=new DOMParser();
>
>
> System.out.println(p.getFeature("http://apache.org/xml/features/allow-java
> -encodings"));
>
>
> p.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
>
>
> System.out.println(p.getFeature("http://apache.org/xml/features/allow-java
> -encodings"));
>
> p.parse(args[0]);
>
> Document doc=p.getDocument();
>
> System.out.println(docToStr(doc));
> }
> catch(Exception e)
> {
> e.printStackTrace();
> }
> }
>
>
>
> public static String docToStr(Document doc) throws Exception
> {
> StringWriter writer = new StringWriter(4096);
> DOMSource source = null;
> String strXML = "";
> StreamResult result = null;
> try
> {
> result = new StreamResult(writer);
> source = new DOMSource(doc);
> TransformerFactory m_tFactory=TransformerFactory.newInstance();
> Transformer m_transformer=m_tFactory.newTransformer();
> m_transformer.transform(source, result);
> strXML = result.getWriter().toString();
> return strXML;
> }
> catch(Exception objEx)
> {
> objEx.printStackTrace();
> throw new Exception("XML_READ_ERROR");
> }
> finally
> {
> // Release references
> writer = null;
> source = null;
> strXML = null;
> result = null;
> }
> }
>
> }
>
>
> **************************************************************************
> *********************************************************
>
> The XML file:
>
> <?xml version="1.0" encoding="UTF-16"?>
> <Demo xmlns="http://www.demo.org/Demo"
> xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
> xsi:schemaLocation="http://www.demo.org/Demo
> Demo.xsd">
> <A>10</A>
> <B>20</B>
> </Demo>
>
>
> **************************************************************************
> *********************************************************
>
>
> The XSD file:
>
> <?xml version="1.0"?>
> <xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
> targetNamespace="http://www.demo.org/Demo"
> xmlns="http://www.demo.org/Demo"
> elementFormDefault="qualified">
> <xsd:element name="Demo">
> <xsd:complexType>
> <xsd:sequence>
> <xsd:element name="A" type="xsd:integer"/>
> <xsd:element name="B" type="xsd:integer"/>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
>
> </xsd:schema>
>
>
>
>
> <<SchemaTest2.java>>
>
> --
> Thanks & Regards,
>
> Subhasis Sanyal
> CATS: Computer Associates (CA) - TCG Software (P) Ltd.
> Bengal Intelligent Park, Building A, Block EP & GP, Sector V,
> Salt Lake Electronics Complex, Kolkata, West Bengal, INDIA - 700091
> Phone: + 91 033 2357 5660 - 65 Extn. 136 (Office)
> Fax: + 91 033 2357 5682
> Email: SubhasisS@catsglobal.co.in
> Visit us at: http://www.catsglobal.com