我有一个文本文件:
engMusic
Anastasia-Te_Iert.mp3
Calvin_Harris_and_Alesso_ft_Hurts-Under_Control.mp3
Oceana-Endless_Summer_Remix.mp3
The_Wanted-Show_Me_Love.mp3
rusMusic
basta-feat-smoki-mo-kamennye-cvety-solovey.su.mp3
maks-barskih-zdes-i-seychas.mp3
我使用SAX解析此文件,我想要像这样获取XML文件
<Music>
<CATALOG_NAME>engMusic</CATALOG_NAME>
<FILE_NAME>Anastasia-Te_Iert.mp3</FILE_NAME>
<FILE_NAME>Calvin_Harris_and_Alesso_ft_Hurts-Under_Control.mp3</FILE_NAME>
<FILE_NAME>Oceana-Endless_Summer_Remix.mp3</FILE_NAME>
<FILE_NAME>The_Wanted-Show_Me_Love.mp3</FILE_NAME>
<CATALOG_NAME>rusMusic</CATALOG_NAME>
<FILE_NAME>basta-feat-smoki-mo-kamennye-cvety-solovey.su.mp3</FILE_NAME>
<FILE_NAME>maks-barskih-zdes-i-seychas.mp3</FILE_NAME>
</Music>
但我有一个问题。我不知道如何选择子文件夹并为“rusMusic”分配标签。实际上我得到了这个输出:
<Music>
<CATALOG_NAME>engMusic</CATALOG_NAME>
<FILE_NAME>Anastasia-Te_Iert.mp3</FILE_NAME>
<FILE_NAME>Calvin_Harris_and_Alesso_ft_Hurts-Under_Control.mp3</FILE_NAME>
<FILE_NAME>Oceana-Endless_Summer_Remix.mp3</FILE_NAME>
<FILE_NAME>The_Wanted-Show_Me_Love.mp3</FILE_NAME>
**<FILE_NAME>rusMusic</FILE_NAME>** // Actually should be <CATALOG_NAME>rusMusic</CATALOG_NAME>
<FILE_NAME>basta-feat-smoki-mo-kamennye-cvety-solovey.su.mp3</FILE_NAME>
<FILE_NAME>maks-barskih-zdes-i-seychas.mp3</FILE_NAME>
</Music>
我的代码:
public class ConvertToXML {
BufferedReader in;
StreamResult out;
TransformerHandler th;
AttributesImpl atts;
public void convertToXml() {
try {
in = new BufferedReader(new FileReader("content.txt"));
out = new StreamResult("dir.xml");
initXML();
String str;
ArrayList<String> content = new ArrayList<>();
while ((str = in.readLine()) != null) {
content.add(str);
}
process(content);
in.close();
writeXML();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void initXML() throws ParserConfigurationException, TransformerConfigurationException, SAXException {
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
th = tf.newTransformerHandler();
Transformer transformer = th.getTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
th.setResult(out);
th.startDocument();
atts = new AttributesImpl();
th.startElement("", "", "Music", atts);
}
private void process(ArrayList<String> elements) throws SAXException {
atts.clear();
th.startElement("", "", "CATALOG_NAME", atts);
th.characters(elements.get(0).toCharArray(), 0, elements.get(0).length());
th.endElement("", "", "CATALOG_NAME");
for (int i = 1; i < elements.size(); i++) {
th.startElement("", "", "FILE_NAME", atts);
th.characters(elements.get(i).toCharArray(), 0, elements.get(i).length());
th.endElement("", "", "FILE_NAME");
}
}
private void writeXML() throws TransformerConfigurationException, TransformerException, SAXException {
th.endElement("", "", "Music");
th.endDocument();
}
}
答案 0 :(得分:0)
我选择这样的事情(没有尝试过,直接在这里输入)
在convertToXml()
while ((str = in.readLine()) != null) {
process(str);
}
process()
现在应该接受String
private void process(String row) throws SAXException {
if (row.startsWith(" ")) {
th.startElement("", "", "FILE_NAME", atts);
th.characters(row.toCharArray(), 0, row.length());
th.endElement("", "", "FILE_NAME");
} else {
th.startElement("", "", "CATALOG_NAME", atts);
th.characters(row.toCharArray(), 0, row.length());
th.startElement("", "", "CATALOG_NAME", atts);
}
}
这可能不会为自己工作,但你明白了。只测试行是否从文件中的任何内容开始(2或4个空格,TAB字符,...),在这种情况下它是FILE_NAME
,否则它是CATALOG_NAME
。