JDOM Chinese, ME! Waiting line

Category: Java EE
 
skyploe
2013-08-20 05:21:28

Sponsored Links
I encountered a problem, is how to change the jdom package default character set (utf-8), the source is:
I do not want to change the program operates, is not to solve the problem, experts advise
import java.io. *;

import javax.xml.transform. *;

import org.jdom. *;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class Article {

. ......
public static Document createDocument () {
/ / Create the root element
Element carElement = new Element ("car");
/ / create the document
Document myDocument = new Document (carElement);
/ / add an attribute to the root element
carElement.addAttribute (new Attribute ("vin", "123fhg5869705iop90"));

/ / add a comment
carElement.addContent (new Comment ("Description of ; a car "));

/ / add some child elements
; / *
* Note that this is the first approach to adding an element and
* textual content. The second approach is commented out.
* /
Element make = new Element ("make");
make.addContent ("Toyota");
carElement.addContent (make);
/ / carElement.addContent ( new Element ("make"). addContent ("Toyota"));

/ / add some more ; elements
carElement.addContent (new Element ("model"). addContent ("Celica"));
; carElement.addContent (new Element ("year"). addContent ("1997"));
carElement.addContent (new Element ("color"). addContent ("green"));
; carElement.addContent (new Element ("license")
. addContent ("1ABC234") . addAttribute ("state", "CA"));

return myDocument;
;}

...
....
public static void executeXSL (Document ; myDocument) {
try {
TransformerFactory tFactory = TransformerFactory.newInstance ();
/ / Make the input sources for the XML and XSLT documents
; org.jdom.output.DOMOutputter outputter = new org.jdom.output.DOMOutputter ();
org.w3c.dom.Document domDocument = outputter.output (myDocument);
javax.xml.transform.Source xmlSource = new javax.xml.transform.dom.DOMSource (domDocument);
StreamSource xsltSource = new StreamSource (new FileInputStream ("xml / car.xsl"));
/ / Make the output result for the finished document
/ *
; * Note that here we are just going to output the results to the
* System.out, since we don't actually have a HTTPResponse object
* in this example
; * /
; / / StreamResult xmlResult = new StreamResult (response.getOutputStream ());
; StreamResult xmlResult = new StreamResult (System.out);
/ / Get a XSLT transformer
Transformer transformer = tFactory.newTransformer (xsltSource);
/ / do the transform
transformer.transform (xmlSource, xmlResult);
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (TransformerConfigurationException e) {
e.printStackTrace ();
} catch (TransformerException e) {
e.printStackTrace ();
} catch (org.jdom.JDOMException e) {
e.printStackTrace ();
}
}

/ **
* Main method that allows the various methods to be used.
* It takes a single command line parameter. If none are
* specified, or the parameter is not understood, it prints
* its usage.
* /
public static void main (String argv [] ) {
if (argv.length == 1) {
; String command = argv [0];
; if (command.equals ("create")) outputDocument (createDocument ());
else if (command.equals ("access")) accessChildElement (createDocument ());
; else if (command.equals ("remove")) removeChildElement (createDocument ());
else if (command.equals ("output")) outputDocument (createDocument ());
; else if (command.equals ("file")) outputDocumentToFile (createDocument ());
else if (command.equals ("read")) outputDocument (readDocument ());
; else if (command.equals ("xsl")) executeXSL (createDocument ());
else {
; System.out.println (command + "is not a valid option.");
printUsage ();
}
} else {
; printUsage ();
}
}
..... ....
}
}

Sponsored Links

shengjiawangba
2013-08-20 05:37:20
Oh, as long as it is OK to set the output encode,
when entered in the DOM object, add this one
outputter.setEncoding ("gb2312");
following example:
public static void outputDocumentToFile (Document myDocument)
{
/ / setup this like outputDocument
try
{
XMLOutputter outputter = new XMLOutputter ("", true);

/ / output to a file
FileWriter writer = new FileWriter ("xml / myFile.xml ");
/ / setup outputDocument enocde
outputter.setEncoding ("gb2312");
outputter.output (myDocument, writer);
writer.close ();

}
catch (java.io.IOException e)
{
e.printStackTrace ();
}
}

pplsdjj
2013-08-20 05:53:53
late step :)

landlord can tie the knot

In addition, such a problem suggested to see more documentation

www.jdom.org
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.