我有一个包含以下条目的文本文件
1
244699000
52.467286666666666
4.611188333333334
我想将其转换为XML文件,如下所示
<?xml version="1.0" encoding="ISO-8859-1"?>
<SYSTEM-TRACKS>
<AIS-SENSOR ID ="1">
<MMSI>244699000 </MMSI>
<LATITUDE> 52.467286666666666 </LATITUDE>
<LONGITUDE> 4.611188333333334 </LONGITUDE>
</AIS-SENSOR>
</SYSTEM-TRACKS>
以下是我找到的代码
public class ToXML {
BufferedReader in;
StreamResult out;
TransformerHandler th;
AttributesImpl atts;
public static void main(String args[]) {
new ToXML().doit();
}
public void doit() {
try {
in = new BufferedReader(new FileReader("data"));
out = new StreamResult("data.xml");
initXML();
String str;
while ((str = in.readLine()) != null) {
process(str);
}
in.close();
closeXML();
} catch (Exception e) {
e.printStackTrace();
}
}
public void initXML() throws ParserConfigurationException,
TransformerConfigurationException, SAXException {
// JAXP + SAX
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory
.newInstance();
th = tf.newTransformerHandler();
Transformer serializer = th.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
// pretty XML output
serializer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "4");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
th.setResult(out);
th.startDocument();
atts = new AttributesImpl();
th.startElement("", "", "SYSTEM-TRACKS", atts);
}
public void process(String s) throws SAXException {
String elements[] = s.split("\\r?\\n");
atts.clear();
th.startElement("", "", "AIS-SENSOR", atts);
th.characters(elements[0].toCharArray(), 0, elements[0].length());
th.startElement("", "", "MMSI", atts);
th.characters(elements[1].toCharArray(), 0, elements[1].length());
th.endElement("", "", "MMSI");
th.startElement("", "", "LATITUDE", atts);
th.characters(elements[2].toCharArray(), 0, elements[2].length());
th.endElement("", "", "LATITUDE");
th.startElement("", "", "LONGITUDE", atts);
th.characters(elements[3].toCharArray(), 0, elements[3].length());
th.endElement("", "", "LONGITUDE");
}
public void closeXML() throws SAXException {
th.endElement("", "", "AIS-SENSOR");
th.endElement("", "", "SYSTEM-TRACKS");
th.endDocument();
}
}
但是当我执行它时会出现以下错误
java.lang.ArrayIndexOutOfBoundsException: 1
at ToXML.process(ToXML.java:76)
at ToXML.doit(ToXML.java:38)
at ToXML.main(ToXML.java:26)
任何人都可以帮我制作一个如上所示的正确XML文件吗?
提前致谢
答案 0 :(得分:2)
使用DOM:
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
DOMImplementation i = b.getDOMImplementation();
Document d = i.createDocument( null , null , null );
然后,只需构建它添加节点:
d.appendChild( d.createElement( ... ) ... );
答案 1 :(得分:0)
您只需从文件中的一行调用流程,然后尝试按新行拆分。你的数组的长度可能是1。
String tmp = "";
while ((str = in.readLine()) != null)
{
tmp += str;
}
process(tmp);
试试这个。
答案 2 :(得分:0)
你的方法很奇怪。您是逐行阅读,然后按换行拆分行,但每行只有一个值,因此您的拆分只返回一个条目。我建议采用这种方法:
String str;
int counter = -1;
while ((str = in.readLine()) != null) {
counter++;
if(counter % 4 == 0) {
process1(str); // add AIS-SENSOR element
continue;
}
if(counter % 4 == 1) {
process2(str); // add MMSI element
continue;
}
if(counter % 4 == 2) {
process3(str); // add LATITUDE element
continue;
}
if(counter % 4 == 3) {
process4(str); // add LONGITUDE element and close AIS-SENSOR
continue;
}
}
counter
会跟踪文件中的当前行。 process1
中的每一个都将根据相应行上的值添加不同的元素。
答案 3 :(得分:0)
首先在doit()中读取你文件中的一行(这意味着你的第一个换行符号) 然后你要在线条上拆分读取线。
因此拆分将始终给出一个长度为1的数组,这会导致在使用索引1(数组中的第二个元素)时发出异常。
你的txt文件应该包含1行所有4行,或者你应该重写代码来处理txt文件。
答案 4 :(得分:0)
将您的文本文件读取为XML,如下所示:
void readXML(){
File textFile=...
Scanner scanner= new Scanner(textFile);
ContentHandler xmlContentHandler xml=...
//make sure to startDocument() with xmlContentHandler
while (scanner.hasNext()){
addStartNodeWithAttribute("AIS-SENSORD", scanner.next());
//move these to a method and use a for loop i=0..<3
xmlContentHandler.startElement("","","MMSI",new AttributesImpl());
String mmsi=scanner.next();
contenthandler.characters(mmsi.toCharArray(), 0, mmsi.length());
contenthandler.endElement("","","MMSI");
xmlContentHandler.startElement("","","LATITUDE",new AttributesImpl());
String lat=scanner.next();
contenthandler.characters(lat.toCharArray(), 0, lat.length());
contenthandler.endElement("","","LATITUDE");
xmlContentHandler.startElement("","","LONGITUDE",new AttributesImpl());
String long=scanner.next();
contenthandler.characters(long.toCharArray(), 0, long.length());
contenthandler.endElement("","","LONGITUDE");
}
//make sure to endDocument() with xmlContentHandler
}
答案 5 :(得分:0)
// This is the original coding with the necessary changes to make it work.
// 1. Out destination is a FileWriter
// 2. Read the 4 lines into an array.
// 3. Process the array.
// patrice_cotte@hotmail.com
package com.my.package;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
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;
import org.xml.sax.helpers.AttributesImpl;
/*
* This program reads a 4 lines text file land creates an XML structure around
* the raw data. The XML structure is then copied to an xml output file.
*/
public class Txt2XML {
BufferedReader in;
StreamResult out;
TransformerHandler th;
AttributesImpl atts;
// Here's our entry point ...
public static void main(String args[]) {
new Txt2XML().doit();
}
public void doit() {
try {
in = new BufferedReader(new FileReader("conf/testdata.txt"));
out = new StreamResult(new FileWriter("conf/testdata.xml"));
initXML();
String str;
// declare an array that can contain 4 strings
String[] SArray=new String[4];
int i = 0;
while ((str = in.readLine()) != null) {
// System.out.println("input line: " + str);
SArray[i]=str;
i++;
}
process(SArray);
in.close();
closeXML();
} catch (Exception e) {
e.printStackTrace();
}
}
public void initXML() throws ParserConfigurationException,
TransformerConfigurationException, SAXException {
// JAXP + SAX
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory
.newInstance();
th = tf.newTransformerHandler();
Transformer serializer = th.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
// pretty XML output
serializer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "4");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
th.setResult(out);
th.startDocument();
atts = new AttributesImpl();
th.startElement("", "", "SYSTEM-TRACKS", atts);
}
public void process(String[] elements) throws SAXException {
// System.out.println("number of elements: " + elements.length);
atts.clear();
th.startElement("", "", "AIS-SENSOR", atts);
th.characters(elements[0].toCharArray(), 0, elements[0].length());
th.startElement("", "", "MMSI", atts);
th.characters(elements[1].toCharArray(), 0, elements[1].length());
th.endElement("", "", "MMSI");
th.startElement("", "", "LATITUDE", atts);
th.characters(elements[2].toCharArray(), 0, elements[2].length());
th.endElement("", "", "LATITUDE");
th.startElement("", "", "LONGITUDE", atts);
th.characters(elements[3].toCharArray(), 0, elements[3].length());
th.endElement("", "", "LONGITUDE");
}
public void closeXML() throws SAXException {
th.endElement("", "", "AIS-SENSOR");
th.endElement("", "", "SYSTEM-TRACKS");
th.endDocument();
}
}