-1

Well When I write it shows strange characters, I have been reading and I have to use FileOutputStream to solve the problem, but I am very new and I do not know how to do it. My code is wrong, there is an error doing, build (xml) and I do not know if I would write the output file in this way.

<?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");  
}

These are the output data

<?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>  

Log Error

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)

I would appreciate your help.

Raúl Puro
  • 35
  • 1
  • 2
  • 7
  • post the content of the file pls – ΦXocę 웃 Пepeúpa ツ Nov 09 '16 at 17:21
  • Done, shows characters, the output xml. – Raúl Puro Nov 09 '16 at 17:32
  • hey... do you need to use JDom??? or can we use a no lib solution?? – ΦXocę 웃 Пepeúpa ツ Nov 09 '16 at 18:07
  • It's not necessary, I used jdom, because it seems simpler, but ... – Raúl Puro Nov 09 '16 at 18:13
  • What *exactly* do you mean by "it shows strange characters"? It's slightly confusing in terms of why you're creating your `out` variable at all, but not writing to it - have you tried passing *that* to `xmlOut.output` instead of creating a new `FileWriter`? (Note that `FileWriter` always uses the platform default encoding, unfortunately.) – Jon Skeet Nov 09 '16 at 19:04
  • Yes, that's the problem, it always uses default encoding, it just does not show the special characters, in Russian all the words are ?????? ... I added the data to the post. Greetings – Raúl Puro Nov 09 '16 at 19:18
  • You say that your code runs and generates bad output? Then you edit the question and add a stacktrace showing that your program is not running and is not generating any output, without explanation of stacktrace. --- Anyway, the stacktrace makes a lot of sense, and shows that question is entirely misleading. `new FileOutputStream(xml)` will open the file for writing, ***truncating*** the file. The next statement, `new SAXBuilder().build(xml)`, will try to read the XML file, but it's now empty and throws SAXParseException with unexpected end of file at line 1, col 1. *Of course it does!!!* – Andreas Nov 09 '16 at 22:11

2 Answers2

2

Depending of the target encoding you have to decide how this will be written to the filesystem. You decided to write with 'UTF8'.

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

You have to make sure that the program which loads the date knows it is encoded in UTF-8. E.g. notepad++ allows to choose a different encoding than the system default. In most cases UTF-8 is not system default. so you have to give the information during loading of the files.

Please also check Java FileReader encoding issue

Community
  • 1
  • 1
Repi
  • 68
  • 4
  • Hi, Yes, that's my idea but I do not know how to do it, I want to encode in UTF 8, the code I wrote in the post, gives an error when executing Document doc = (Document) new SAXBuilder (). Build (xml) , Says that the format is not correct. Greetings. – Raúl Puro Nov 09 '16 at 20:20
  • Did you try to use "UTF-8" instead of "UTF8"? Or can you provide a stacktrace. – Repi Nov 09 '16 at 20:30
  • 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) – Raúl Puro Nov 09 '16 at 20:54
  • I added the full log in post. Greetings and thanks. – Raúl Puro Nov 09 '16 at 21:05
  • 1
    Is it correct that you want tro read and want to write the the "File xml" at the same time? – Repi Nov 09 '16 at 21:34
  • Hi, yes, that's the idea, when id = 1 or 2, I have to modify the values, is there another simpler way to do it ?, I'm still rookie, greetings. – Raúl Puro Nov 09 '16 at 21:46
  • I think you are right, if I use another output xml does not give error, it is possible that the solution is to create an xml with the identical structure, and with the values already changed but that is very difficult for me. – Raúl Puro Nov 09 '16 at 22:00
  • Did you try to first read from file new SAXBuilder().build(xml); Update the Document and at the end write the whole document to the output stream. The usage of out.append() will not work in this case. Try to use two different files for in and output. The error seems to point to an empty document. – Repi Nov 09 '16 at 22:01
  • this should help with writing DOM problem [link](https://docs.oracle.com/javase/tutorial/jaxp/xslt/writingDom.html) – Repi Nov 09 '16 at 22:06
  • @LarsRepenning See [my comment to question](http://stackoverflow.com/questions/40512622/java-xml-error-encoding-utf-8#comment68275392_40512622). You are right, it's an empty file and this question has nothing whatsoever to do with file encoding. – Andreas Nov 09 '16 at 22:14
  • Hi, Andreas, of course, both of you are right, and now when you have explained it has all the logic, but now I'm totally lost, i do not know how to do it, in this post mine http://stackoverflow.com/questions/40500027/error-change-values-in-node-of-a-xml , it does well, but I have the coding problem, SAXBuilder reads And modifies at the same time, but I have not found how to encode the file to utf-8. This is killing me ... greetings – Raúl Puro Nov 09 '16 at 22:33
1

A example file with this content:

<?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>

can be parsed using DOM native java

Example:

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")));
    }
    }
}
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Thanks a lot, I have some doubts, now to modify values to the attributes ?, eElement.getAttribute ( "desc"). SetValue ( "raúl") does not exist, to save what should I do ?, I have never used nodes, this is my exit Amount, id 1, val -1, num, Desc résd Amount, id 5, val -1, num, Desc .. ?? ? ?????? ? ?????????? ? ???? Amount, id 1, val -1, num, Desc hi Amount, id 5, val -1, num, Desc Hello – Raúl Puro Nov 09 '16 at 19:50
  • Hi, does not encode Russian characters well, are you Russian ?, you may be using your local coding. Greetings. – Raúl Puro Nov 09 '16 at 19:57
  • *FYI:* This question has [nothing to do with encoding](http://stackoverflow.com/questions/40512622/java-xml-error-encoding-utf-8#comment68275392_40512622). – Andreas Nov 09 '16 at 22:15