Friday 15 June 2012

java - The structure of the xml file -



java - The structure of the xml file -

i have 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 file using sax , want xml file 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 have problem. don't know how select subfolder , assign tag "rusmusic". got 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>** // should <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() { seek { 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(); } grab (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"); (int = 1; < 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 (didn't seek it, typing here directly)

in converttoxml()

while ((str = in.readline()) != null) { process(str); }

and process() should take 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 won't work itself, idea. test if row starts whatever starting in file (2 or 4 spaces, tab character, ...) in case file_name, else catalog_name.

java xml

No comments:

Post a Comment