...">

XML conversion and line break characters

ok, I have some code that works on this xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<record-table>  
    <record>
        <record_id>1</record_id>
        <record_rows>
          <record_row>abcdef</record_row>
        </record_rows>
      </record>
      <record>
        <record_id>2</record_id>
        <record_rows>
          <record_row>abcdef</record_row>
          <record_row>abcdef</record_row>
        </record_rows>
      </record>
</record-table>  

The code splits the source XML file into 2 files, and then tries to add some tags.

import java.io.*;
import java.io.FileReader;
import java.io.FileWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stax.StAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.stream.XMLStreamException;

import java.io.FileOutputStream;
import javax.xml.transform.OutputKeys;


public class ver2 {

    public static void main(String[] args) throws Exception  {
        XMLInputFactory xif = XMLInputFactory.newInstance();
        xif.setProperty("javax.xml.stream.isCoalescing", true);
        XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("out.xml"));

        XMLOutputFactory factory = XMLOutputFactory.newInstance();

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();

        xsr.nextTag(); 
        int i=0;
        while(xsr.hasNext()) {
            int event = xsr.next();
            if (event== XMLStreamConstants.START_ELEMENT){
                        if (xsr.getLocalName().equals("record")){
                        i++;
                        File file = new File(i + ".txt");

                             try {
                             XMLStreamWriter writer = factory.createXMLStreamWriter(new   FileWriter(file));

                             t.transform(new StAXSource(xsr), new StAXResult(writer)); 

                            writer.writeStartElement("addSomeTags");
                            writer.writeCharacters("\r\n");
                            writer.writeStartElement("somestuff");

                            writer.writeEndElement();
                            writer.writeEndElement();


                            writer.flush();
                            writer.close();

                         } catch (XMLStreamException e) {
                             e.printStackTrace();
                         } catch (IOException e) {
                             e.printStackTrace();
                         }

                    }
            }
        }
    }
} 

The problem is that when I open the resulting files in notepad, the notepad does not recognize the queue characters. (probably because they are "/ n" and not "/ n / r"). At the same time, if I converted StaxSource to StreamResult (without using XMLStreamWriter), notepad will recognize them. Can you explain why this happens and how to make it well formed?

(but, as you understand, this is correctly represented in WordPad or other text editors, including this one)

Also , if this method is somehow perverted, please let me know.

+3
3

writer.writeCharacters("\r\n"); 

write.writeCharacters(System.getProperty("line.separator"));

\r\n, .

+2

. , , ; .

    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

, ? , XML , . Windows 7 Notepad ..

    Reader in = new StringReader("<a><b>Hello</b><c><d>World</d></c></a>");
    Writer out = new FileWriter("C:\\Temp\\test.xml");

    XMLInputFactory xif = XMLInputFactory.newInstance();
    xif.setProperty("javax.xml.stream.isCoalescing", true);
    XMLStreamReader xin = xif.createXMLStreamReader(in);

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();

    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

    t.transform(new StAXSource(xin), new StreamResult(out));
0

You might be better off changing the encoding in the file.

FileOutputStream XmlOutputStream = new FileOutputStream(...);
Writer XmlWriterUtf8 = new OutputStreamWriter(XmlOutputStream, "UTF8");
XmlWriterUtf8.write(XmlBuffer.toString());
XmlWriterUtf8.close();

When working with Windows, the correct encoding for storing the file is not necessarily used.

0
source

All Articles