cómo convertir un archivo de texto a xml

Quiero convertir el text file dentro xml file.I have a large amount of string but i dont want to write in xml directly.

So that I have made a text file now i want to convert this

text file into xml format but when i am running this file getting no output. here is my

código:

public void convert() throws Exception {

 String text[]=new String[10];

 FileOutputStream fout = new FileOutputStream("res/values/mysml.xml");

 OutputStreamWriter out = new OutputStreamWriter(fout);  

 InputStream in= getAssets().open("myText.txt");

 Scanner scn = new Scanner(is);

for(int i=0;i<10;i++)

text[i]=bin.readLine();

out.write("<?xml version=\"1.0\"?>\r\n");  

out.write("<resources>\r\n");  

for (int i = 0; i < 10; i++){

out.write("<item>"+text[i]+"</item>");

}

out.write("</resources>");

out.flush();

out.close();

}

preguntado el 31 de julio de 12 a las 11:07

Have you tried calling out.flush() before closing the stream? -

where you are closing first for loop? -

close() will flushing it first. -

i am getting an exception at this line while debugging. FileOutputStream fout = new FileOutputStream("res/values/mysml.xml"); -

@sachit Even if you made the folder you do not have permission to write anything into it. Also, even if you could be able to write in the file, Android will not recognize your written contents as resources. I strongly reccomend you to use a different approach such as Internal or External Memory or Shared Preferences. -

6 Respuestas

From what i understand, you want to create dynamically during runtime an xml in your app's res/values/ folder. I do not think this is possible since you only have read access in your app's resources. Instead of storing your xml into your res folder, try the internal or external memmory. See aquí for more information about android storage options.

Respondido 31 Jul 12, 11:07

Si tienen to do that, use StAX. You can open close tags in a easier way and the writer will take care of your File as well.

XMLOutputFactory output = XMLOutputFactory.newInstance();
XMLStreamWriter writer = output.createXMLStreamWriter(new BufferedWriter(new FileWriter(new File(FILE_PATH))));
writer.writeStartDocument("UTF-8","1.0");
// Write Whatever file you have as string
writer.flush();
writer.close();

And StAX ships with JDK 1.6

Respondido 31 Jul 12, 11:07

Trata

BufferedReader reader=new BufferedReader(new InputStreamReader(in));

String line=null;
out.write("<?xml version=\"1.0\"?>\r\n");  
out.write("<resources>\r\n");  
while((line=reader.readLine())!=null)
{
  out.write("<item>"+line+"</item>");
}
out.write("</resources>");
out.close();

Respondido 31 Jul 12, 11:07

to flush to stream add this line here:

out.write("</resources>");
//line
out.flush();

out.close();

Respondido 31 Jul 12, 11:07

for(int i=0;i<10;i++){
    text[i]=bin.readLine();
} <-----

out.write("<?xml version=\"1.0\"?>\r\n");  

out.write("<resources>\r\n"); 



for (int i = 0; i < 10; i++){
    out.write("<item>"+text[i]+"</item>");
}

Respondido 01 ago 12, 23:08

Check your site with http://www.xml-sitemaps.com/.

Make changes in the scroll window, say add html files in subfolders. Put coding in it as you see already there.

Do not save the XML file from the webpage. It will give you the unchanged code in the window.

copy the text from the scroll window. Paste it into a blank notepad page.

Save it as "sitemap.xml".

Remember, put the quote markes to save as an xml file.

Respondido 14 Feb 13, 13:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.