我看到这个代码发布在另一个线程上,但无法通过转换文本文件将其正确格式化为带有子元素的xml。而不是输出简单的xml
由此:
Example One
Example Two
Example Three
对此:
<?xml version="1.0" encoding="UTF-8"?>
<inserts>
<option>Example One</option>
<option>Example Two</option>
<option>Example Three</option>
</inserts>
我需要从文本文件中进行以下转换:
first_main_elem
f_child_elem1:one
f_child_elem2:two
second_main_elem
s_child_elem1:one
s_child_elem1:two
third_standalone_element: null value
fourth_element:stand alone
为:
<?xml version="1.0" encoding="UTF-8"?>
<first_main_elem>
<f_child_elem1>one</f_child_elem1>
<f_child_elem1>two</f_child_elem1>
<f_child_elem1>three</f_child_elem1>
</first_main_element>
<second_main_elem>
<s_child_elem1>one</s_child_elem>
<s_child_elem2>two</s_child_elem2>
</second_main_elem>
<fourth_standalone_elem/>
<fifth_element>stand alone</fifth_element>
发布的代码:
import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.sax.*;
public class ToXML {
BufferedReader in;
StreamResult out;
TransformerHandler th;
public static void main(String args[]) {
new ToXML().begin();
}
public void begin() {
try {
in = new BufferedReader(new FileReader("data.txt"));
out = new StreamResult("data.xml");
openXml();
String str;
while ((str = in.readLine()) != null) {
process(str);
}
in.close();
closeXml();
} catch (Exception e) {
e.printStackTrace();
}
}
public void openXml() throws ParserConfigurationException,TransformerConfigurationException, SAXException{
SAXTransformerFactory tf = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
th = tf.newTransformerHandler();
// pretty XML output
Transformer serializer = th.getTransformer();
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
th.setResult(out);
th.startDocument();
th.startElement(null, null, "inserts", null);
}
public void process(String s) throws SAXException {
th.startElement(null, null, "option", null);
th.characters(s.toCharArray(), 0, s.length());
th.endElement(null, null, "option");
}
public void closeXml() throws SAXException {
th.endElement(null, null, "inserts");
th.endDocument();
}
}
感谢您对此提出任何建议或意见。
答案 0 :(得分:0)
草案版本请在下面找到,但要更多地了解解析空节点:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;
public class ToXML {
BufferedReader in;
StreamResult out;
TransformerHandler th;
public static void main(String args[]) {
new ToXML().begin();
}
public void begin() {
try {
in = new BufferedReader(new FileReader(
"data.txt"));
out = new StreamResult("data.xml");
openXml();
String str;
String[] elements = null;
// skip first line in input file
in.readLine();
while ((str = in.readLine()) != null) {
if (str.contains(":")) {
elements = str.split(":");
process(elements[0], elements[1]);
} else {
th.startElement(null, null, str, null);
}
}
in.close();
closeXml("first_main_elem");
} catch (Exception e) {
e.printStackTrace();
}
}
public void openXml() throws ParserConfigurationException,
TransformerConfigurationException, SAXException {
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory
.newInstance();
th = tf.newTransformerHandler();
// pretty XML output
Transformer serializer = th.getTransformer();
serializer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "4");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
th.setResult(out);
th.startDocument();
th.startElement(null, null, "first_main_elem", null);
}
public void process(String tag, String s) throws SAXException {
th.startElement(null, null, tag, null);
th.characters(s.toCharArray(), 0, s.length());
th.endElement(null, null, tag);
}
public void closeXml(String s) throws SAXException {
th.endElement(null, null, s);
th.endDocument();
}
}