[英]The structure of the xml file
I have a text file: 我有一个文本文件:
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
I parse this file using SAX and i want get XML file like this 我使用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>
But i have a problem. 但是我有一个问题。 I don't know how select subfolder and assign a tag for "rusMusic". 我不知道如何选择子文件夹并为“ rusMusic”分配标签。 Actually i got this output: 其实我得到了这个输出:
<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>
My Code: 我的代码:
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();
}
} }
I'd go with something like this (didn't try it, typing it here directly) 我会喜欢这样的东西(没有尝试过,直接在这里输入)
In convertToXml()
在convertToXml()
while ((str = in.readLine()) != null) {
process(str);
}
And process()
should now accept String
并且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);
}
}
This probably won't work for itself, but you get the idea. 这可能自己行不通,但是您明白了。 Just test if row starts with whatever it is starting in your file (2 or 4 spaces, TAB character, ...) in which case it is FILE_NAME
, else it is CATALOG_NAME
. 只需测试row是否以文件中的开头(2或4个空格,TAB字符,...)开头,在这种情况下为FILE_NAME
,否则为CATALOG_NAME
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.