我需要帮助将文本文件转换为xml文件。
这就是我想要转换后的文本文件的样子:
<ItemBonusDefinition>
<id>88</id>
<bonuses>
<short>1</short>
<short>1</short>
<short>1</short>
</bonuses>
</ItemBonusDefinition>
<ItemBonusDefinition>
<id>89</id>
<bonuses>
<short>1</short>
<short>1</short>
<short>1</short>
</bonuses>
</ItemBonusDefinition>
这是文本文件的外观:
88
1.0
1.0
1.0
89
1.0
1.0
1.0
这是我的代码:
package com.test.tools;
import java.io.BufferedReader;
import java.io.FileReader;
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.dom4j.Element;
import org.xml.sax.SAXException;
import com.arlania.signlink;
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(signlink.getCacheDirectory()+"items2.txt"));
out = new StreamResult(signlink.getCacheDirectory()+"items4.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, "list", null);
th.startElement(null, null, "ItemBonusDefinition", null);
}
/**
* Lists all the ints in <id> and the double in <short>. id are item id's and short is the bonus for the item id's
* @param s
* @throws SAXException
*/
public void process(String s) throws SAXException {
if (isInteger(s)) {
th.startElement(null, null, "id", null);
th.characters(s.toCharArray(), 0, s.length());
th.endElement(null, null, "id");
} else if (isDouble(s)) {
th.startElement(null, null, "short", null);
th.characters(s.toCharArray(), 0, s.length());
th.endElement(null, null, "short");
}
}
public void closeXml() throws SAXException {
th.endElement(null, null, "ItemBonusDefinition");
th.endElement(null, null, "list");
th.endDocument();
}
public boolean isDouble( String str ){
try{
Double.parseDouble( str );
return true;
}
catch( Exception e ){
return false;
}
}
public boolean isInteger(Object object) {
if(object instanceof Integer) {
return true;
} else {
String string = object.toString();
try {
Integer.parseInt(string);
} catch(Exception e) {
return false;
}
}
return true;
}
}
代码输出如下:
<ItemBonusDefinition>
<id>88</id>
<short>1.0</short>
<short>1.0</short>
<short>1.0</short>
<id>89</id>
<short>1.0</short>
<short>1.0</short>
<short>1.0</short>
我需要帮助,如在第一个示例中一样正确添加<bonuses>
,也需要添加<ItemBonusDefinition>
。它必须看起来像第一个示例一样。我将不胜感激:)
答案 0 :(得分:0)
尝试此dom4j代码,它可以生成所需的xml文件。
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Convert {
private static final int LINES_PER_ELEMENT = 4;
private static final int COUNT_OF_ELEMENTS = 2;
private static final int ID_INDEX = 0;
public static List<String> getFile() {
List<String> list = new ArrayList<String>();
File file = new File("text");
BufferedReader br = null;
try {
InputStream in = new FileInputStream(file);
Reader reader = new InputStreamReader(in);
br = new BufferedReader(reader);
String line = null;
while((line = br.readLine()) != null) {
list.add(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return list;
}
public static Document getXml(List<String> lines) {
Element root = DocumentHelper.createElement("root");
Document document = DocumentHelper.createDocument(root);
String[][] strArr = new String[COUNT_OF_ELEMENTS][LINES_PER_ELEMENT];
for(int i = 0;i < COUNT_OF_ELEMENTS;i++) {
for(int j = 0;j < LINES_PER_ELEMENT;j++) {
strArr[i][j] = lines.get((i + 1) * (j + 1) - 1);
}
}
for(int i = 0;i < COUNT_OF_ELEMENTS;i++) {
Element itemBonusDefinition = root.addElement("ItemBonusDefinition");
String[] elemValues = strArr[i];
Element id = itemBonusDefinition.addElement("id");
Element bonus = itemBonusDefinition.addElement("bonus");
for(int j = 0;j < elemValues.length;j++) {
if(j == ID_INDEX) {
id.setText(elemValues[j]);
} else {
Element shorT = bonus.addElement("short");
shorT.setText(elemValues[j]);
}
}
}
return document;
}
public static void main(String[] args) throws IOException {
List<String> lines = getFile();
Document document = getXml(lines);
OutputStream out = new FileOutputStream("C:\\Users\\admin\\Desktop\\test11.xml");
Writer wr = new OutputStreamWriter(out);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(wr,format);
writer.write(document);
writer.close();
}
}
结果是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<ItemBonusDefinition>
<id>88</id>
<bonus>
<short>1.0</short>
<short>1.0</short>
<short>1.0</short>
</bonus>
</ItemBonusDefinition>
<ItemBonusDefinition>
<id>1.0</id>
<bonus>
<short>1.0</short>
<short>1.0</short>
<short>1.0</short>
</bonus>
</ItemBonusDefinition>
</root>