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
<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". 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()
while ((str = in.readLine()) != null) {
process(str);
}
And process()
should now accept 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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.