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



<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://ift.tt/1fh1NGB", "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();
}


}


No comments:

Post a Comment