Java xml set encoding

Java xml set encoding

The XMLEncoder class is a complementary alternative to the ObjectOutputStream and can used to generate a textual representation of a JavaBean in the same way that the ObjectOutputStream can be used to create binary representation of Serializable objects. For example, the following fragment can be used to create a textual representation the supplied JavaBean and all its properties:

XMLEncoder e = new XMLEncoder( new BufferedOutputStream( new FileOutputStream("Test.xml"))); e.writeObject(new JButton("Hello, world")); e.close();
  • Portable and version resilient: they have no dependencies on the private implementation of any class and so, like Java source files, they may be exchanged between environments which may have different versions of some of the classes and between VMs from different vendors.
  • Structurally compact: The XMLEncoder class uses a redundancy elimination algorithm internally so that the default values of a Bean’s properties are not written to the stream.
  • Fault tolerant: Non-structural errors in the file, caused either by damage to the file or by API changes made to classes in an archive remain localized so that a reader can report the error and continue to load the parts of the document which were not affected by the error.

Below is an example of an XML archive containing some user interface components from the swing toolkit:

    frame1   0 0 200 200      Hello     true    
  • Each element represents a method call.
  • The «object» tag denotes an expression whose value is to be used as the argument to the enclosing element.
  • The «void» tag denotes a statement which will be executed, but whose result will not be used as an argument to the enclosing method.
  • Elements which contain elements use those elements as arguments, unless they have the tag: «void».
  • The name of the method is denoted by the «method» attribute.
  • XML’s standard «id» and «idref» attributes are used to make references to previous expressions — so as to deal with circularities in the object graph.
  • The «class» attribute is used to specify the target of a static method or constructor explicitly; its value being the fully qualified name of the class.
  • Elements with the «void» tag are executed using the outer context as the target if no target is defined by a «class» attribute.
  • Java’s String class is treated specially and is written Hello, world where the characters of the string are converted to bytes using the UTF-8 character encoding.
  • The default method name is «new».
  • A reference to a java class is written in the form javax.swing.JButton.
  • Instances of the wrapper classes for Java’s primitive types are written using the name of the primitive type as the tag. For example, an instance of the Integer class could be written: 123. Note that the XMLEncoder class uses Java’s reflection package in which the conversion between Java’s primitive types and their associated «wrapper classes» is handled internally. The API for the XMLEncoder class itself deals only with Object s.
  • In an element representing a nullary method whose name starts with «get», the «method» attribute is replaced with a «property» attribute whose value is given by removing the «get» prefix and decapitalizing the result.
  • In an element representing a monadic method whose name starts with «set», the «method» attribute is replaced with a «property» attribute whose value is given by removing the «set» prefix and decapitalizing the result.
  • In an element representing a method named «get» taking one integer argument, the «method» attribute is replaced with an «index» attribute whose value the value of the first argument.
  • In an element representing a method named «set» taking two arguments, the first of which is an integer, the «method» attribute is replaced with an «index» attribute whose value the value of the first argument.
  • A reference to an array is written using the «array» tag. The «class» and «length» attributes specify the sub-type of the array and its length respectively.
Читайте также:  Develop website with html

For more information you might also want to check out Using XMLEncoder, an article in The Swing Connection.

Constructor Summary

Creates a new XML encoder to write out JavaBeans to the stream out using the given charset starting from the given indentation .

Method Summary

This method calls flush , writes the closing postamble and then closes the output stream associated with this stream.

This method writes out the preamble associated with the XML encoding if it has not been written already and then writes out all of the values that been written to the stream since the last time flush was called.

Records the Expression so that the Encoder will produce the actual output when the stream is flushed.

Источник

Java, XML DocumentBuilder — setting the encoding when parsing

Here’s an updated answer since OutputFormat is deprecated :

TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); 

The second part will return the XML Document as String

Solution 2

// Read XML String xml = "xml" DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(xml))); // Append formatting OutputFormat format = new OutputFormat(document); if (document.getXmlEncoding() != null) < format.setEncoding(document.getXmlEncoding()); >format.setLineWidth(100); format.setIndenting(true); format.setIndent(5); Writer out = new StringWriter(); XMLSerializer serializer = new XMLSerializer(out, format); serializer.serialize(document); String result = out.toString(); 

Solution 3

I solved it, given alot of trial and errors.

OutputFormat format = new OutputFormat(document); 
OutputFormat format = new OutputFormat(d, encoding, true); 

and this solved my problem.

encoding is what I set it to be
true refers to whether or not indent is set.

Note to self — read more carefully — I had looked at the javadoc hours ago — if only I’d have read more carefully.

Источник

Class XMLEncoder

The XMLEncoder class is a complementary alternative to the ObjectOutputStream and can used to generate a textual representation of a JavaBean in the same way that the ObjectOutputStream can be used to create binary representation of Serializable objects. For example, the following fragment can be used to create a textual representation the supplied JavaBean and all its properties:

XMLEncoder e = new XMLEncoder( new BufferedOutputStream( new FileOutputStream("Test.xml"))); e.writeObject(new JButton("Hello, world")); e.close();

Despite the similarity of their APIs, the XMLEncoder class is exclusively designed for the purpose of archiving graphs of JavaBeans as textual representations of their public properties. Like Java source files, documents written this way have a natural immunity to changes in the implementations of the classes involved. The ObjectOutputStream continues to be recommended for interprocess communication and general purpose serialization.

  • Portable and version resilient: they have no dependencies on the private implementation of any class and so, like Java source files, they may be exchanged between environments which may have different versions of some of the classes and between VMs from different vendors.
  • Structurally compact: The XMLEncoder class uses a redundancy elimination algorithm internally so that the default values of a Bean’s properties are not written to the stream.
  • Fault tolerant: Non-structural errors in the file, caused either by damage to the file or by API changes made to classes in an archive remain localized so that a reader can report the error and continue to load the parts of the document which were not affected by the error.

Below is an example of an XML archive containing some user interface components from the swing toolkit:

    frame1   0 0 200 200      Hello     true    
  • Each element represents a method call.
  • The «object» tag denotes an expression whose value is to be used as the argument to the enclosing element.
  • The «void» tag denotes a statement which will be executed, but whose result will not be used as an argument to the enclosing method.
  • Elements which contain elements use those elements as arguments, unless they have the tag: «void».
  • The name of the method is denoted by the «method» attribute.
  • XML’s standard «id» and «idref» attributes are used to make references to previous expressions — so as to deal with circularities in the object graph.
  • The «class» attribute is used to specify the target of a static method or constructor explicitly; its value being the fully qualified name of the class.
  • Elements with the «void» tag are executed using the outer context as the target if no target is defined by a «class» attribute.
  • Java’s String class is treated specially and is written Hello, world where the characters of the string are converted to bytes using the UTF-8 character encoding.
  • The default method name is «new».
  • A reference to a java class is written in the form javax.swing.JButton.
  • Instances of the wrapper classes for Java’s primitive types are written using the name of the primitive type as the tag. For example, an instance of the Integer class could be written: 123. Note that the XMLEncoder class uses Java’s reflection package in which the conversion between Java’s primitive types and their associated «wrapper classes» is handled internally. The API for the XMLEncoder class itself deals only with Object s.
  • In an element representing a nullary method whose name starts with «get», the «method» attribute is replaced with a «property» attribute whose value is given by removing the «get» prefix and decapitalizing the result.
  • In an element representing a monadic method whose name starts with «set», the «method» attribute is replaced with a «property» attribute whose value is given by removing the «set» prefix and decapitalizing the result.
  • In an element representing a method named «get» taking one integer argument, the «method» attribute is replaced with an «index» attribute whose value the value of the first argument.
  • In an element representing a method named «set» taking two arguments, the first of which is an integer, the «method» attribute is replaced with an «index» attribute whose value the value of the first argument.
  • A reference to an array is written using the «array» tag. The «class» and «length» attributes specify the sub-type of the array and its length respectively.

For more information you might also want to check out Using XMLEncoder, an article in The Swing Connection.

Источник

Оцените статью