Java xml utf-8 编码错误

Java xml error encoding utf-8

Well When I write it shows strange characters, 我一直在看,我不得不使用FileOutputStream来解决这个问题,但是我很新,我不知道该怎么做。我的代码是错误的,执行时出错,构建(xml),我不知道我是否会这样写输出文件。

<?xml version="1.0" encoding="UTF-8"?>
 <prueba>
     <reg id="576340">
           <dato cant="680" id="1" val="-1" num="" desc="résd" />
           <dato cant="684" id="5" val="-1" num="" desc="да и вообще" /> 
           <dato cant="1621" id="1" val="-1" num="" desc="Hi" />
           <dato cant="1625" id="5" val="-1" num="" desc="Hola" />  
     </reg>
 </prueba>


public static void main(String[] args) throws FileNotFoundException, 
     JDOMException, IOException {

SAXBuilder builder = new SAXBuilder();
File xml = new File("c:\prueba3.xml");
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(xml), "UTF8"));
Document doc = (Document) new SAXBuilder().build(xml);
Element raiz = doc.getRootElement();
List articleRow = raiz.getChildren("reg"); 

for (int i = 0; i < articleRow.size(); i++) {

    Element row = (Element) articleRow.get(i);
    List images = row.getChildren("dato");

     for (int j = 0; j < images.size(); j++) {

         Element row2 = (Element) images.get(j);
         String texto = row2.getAttributeValue("desc") ;
         String id = row2.getAttributeValue("id"); 

         if ((texto != null) && (texto !="") && 
            (id.equals("1") || id.equals("2"))){                   

         //row2.getChild("desc").setText("valor");   
         out.append(row2.getAttribute("desc").setValue.
                   ("raúl").toString());
         }
     }
}
 out.flush();
 out.close();
 System.out.println("fin de programa");  
}

这些是输出数据

<?xml version="1.0" encoding="UTF-8"?>
 <prueba>
    <reg id="576340">
           <dato cant="680" id="1" val="-1" num="" desc="ra/>
           <dato cant="684" id="5" val="-1" num="" desc="..?? ? ??????/>
           <dato cant="1621" id="1" val="-1" num="" desc="ra/>
           <dato cant="1625" id="5" val="-1" num="" desc="Hola" />
    </reg>
  </prueba>  

日志错误

Exception in thread "main" org.jdom.input.JDOMParseException: Error on line 1 of document file:/c:/prueba3.xml: Final de archivo prematuro.
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:530)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:905)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:884)
at Prueba.main(Prueba.java:27)Caused by: org.xml.sax.SAXParseException; systemId: file:/c:/prueba3.xml; lineNumber: 1; columnNumber: 1; Final de archivo prematuro.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
... 3 moreCaused by: org.xml.sax.SAXParseException; systemId: file:/c:/prueba3.xml; lineNumber: 1; columnNumber: 1; Final de archivo prematuro.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:905)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:884)
at Prueba.main(Prueba.java:27)

非常感谢您的帮助。

具有以下内容的示例文件:

<?xml version="1.0" encoding="UTF-8"?>
 <prueba>
     <reg id="123456">
           <dato cantidad="680" id="1" val="-1" num="" desc="résd" />
           <dato cantidad="684" id="5" val="-1" num="" desc="да и вообще" /> 
           <dato cantidad="1621" id="1" val="-1" num="" desc="Hi" />
           <dato cantidad="1625" id="5" val="-1" num="" desc="Hola" />  
     </reg>
 </prueba>

可以使用DOM原生解析java

示例:

public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
    final File fXmlFile = new File("./Details2.xml");
    final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    final Document document = dBuilder.parse(fXmlFile);
    document.getDocumentElement().normalize();
    final NodeList regNodeList = document.getElementsByTagName("reg");
    for (int counter = 0; counter < regNodeList.getLength(); counter++) {
        final Node nNode = regNodeList.item(counter);
        System.out.println("Current Element :" + nNode.getNodeName());
        System.out.println("regs id : " + ((Element) nNode).getAttribute("id"));
        final NodeList nList2 = ((Element) nNode).getElementsByTagName("dato");

        for (int counterChilds = 0; counterChilds < nList2.getLength(); counterChilds++) {
        final Node nNode2 = nList2.item(counterChilds);
        if (nNode2.getNodeType() == Node.ELEMENT_NODE) {
            final Element eElement = (Element) nNode2;
            System.out.println(String.format("Cantidad %s,id %s,val %s,num %s,Desc %s",
            eElement.getAttribute("cantidad"), eElement.getAttribute("id"),
            eElement.getAttribute("val"), eElement.getAttribute("num"), eElement.getAttribute("desc")));
    }
    }
}
}

根据目标编码,您必须决定如何将其写入文件系统。你决定写 'UTF8'.

Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xml), "UTF8"));

您必须确保加载日期的程序知道它是用 UTF-8 编码的。例如。 notepad++ 允许选择不同于系统默认的编码。在大多数情况下,UTF-8 不是系统默认值。所以你必须在加载文件时提供信息。

请同时检查Java FileReader encoding issue