Hallo würde gerne diese zwei Methoden verbinden. Aber irgendwie bekomm ich es nicht hin.
Es handelt sich ums einlesen einer XML Datei, die anschließend formatiert ausgegeben wird.
Es handelt sich ums einlesen einer XML Datei, die anschließend formatiert ausgegeben wird.
Java:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class HostXMLStatementConv
{
private BufferedReader fi;
private BufferedWriter fo;
private String line;
public HostXMLStatementConv(String datei_ein, String datei_aus)
{
try
{
StringBuilder all_content = new StringBuilder();
this.fi = new BufferedReader(new FileReader(datei_ein));
this.fo = new BufferedWriter(new FileWriter(datei_aus));
while ((this.line = this.fi.readLine()) != null)
{
all_content.append(line);
}
this.fi.close();
this.fo.close();
}
catch (IOException e)
{
System.out.println("Fehler beim Lesen/Schreiben einer Datei " + e);
}
}
}
Java:
import org.w3c.dom.Node;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
/**
* Pretty-prints xml, supplied as a string.
* <p/>
* eg.
* <code>
* String formattedXml = new XmlFormatter().format("<tag><nested>hello</nested></tag>");
* </code>
*/
public class Main {
public String format(String xml) {
try {
final InputSource src = new InputSource(new StringReader(xml));
final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));
//May need this: System.setProperty(DOMImplementationRegistry.PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
final LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.
writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.
return writer.writeToString(document);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String unformattedXml ="";
System.out.println(new Main().format(unformattedXml));
}
}