java - Error when trying to write to an XML file -



java - Error when trying to write to an XML file -

i'm trying read csv file online , convert xml code. using variation of this answer, maintain getting same error:

invalid_character_err: invalid or illegal xml character specified.

here code have:

import java.io.*; import java.net.url; import java.net.urlconnection; import java.util.*; import javax.swing.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.domsource; import javax.xml.transform.stream.streamresult; import org.w3c.dom.element; import javax.naming.spi.dirstatefactory.result; import org.w3c.dom.document; public class xmlcreator { documentbuilderfactory domfactory = null; documentbuilder dombuilder = null; public static void main(string[] args) { xmlcreator xml = new xmlcreator(); xml.convertfile("testthis", ""); } public xmlcreator() { seek { domfactory = documentbuilderfactory.newinstance(); dombuilder = domfactory.newdocumentbuilder(); } grab (factoryconfigurationerror exp) { system.err.println(exp.tostring()); } grab (parserconfigurationexception exp) { system.err.println(exp.tostring()); } grab (exception exp) { system.err.println(exp.tostring()); } } public int convertfile(string xmlfilename, string delimiter) { int rowscount = -1; seek { document newdoc = dombuilder.newdocument(); // root element element rootelement = newdoc.createelement("xmlcreator"); newdoc.appendchild(rootelement); // read csv file bufferedreader csvreader; //csvreader = new bufferedreader(new filereader("http://example.com/public/ex.csv")); url csvurl = new url("http://example.com/public/ex.csv"); urlconnection urlcon = (urlconnection) csvurl.openconnection(); inputstreamreader instream = new inputstreamreader(urlcon.getinputstream()); csvreader = new bufferedreader(instream); int line = 0; list<string> headers = new arraylist<string>(5); string text = null; while ((text = csvreader.readline()) != null) { stringtokenizer st = new stringtokenizer(text, delimiter, false); string[] rowvalues = new string[st.counttokens()]; int index = 0; while (st.hasmoretokens()) { string next = st.nexttoken(); rowvalues[index++] = next; } if (line == 0) { // header row (string col : rowvalues) { headers.add(col); } } else { // info row rowscount++; element rowelement = newdoc.createelement("row"); rootelement.appendchild(rowelement); (int col = 0; col < headers.size(); col++) { string header = headers.get(col); string value = null; if (col < rowvalues.length) { value = rowvalues[col]; } else { // ?? default value value = ""; } system.out.println(header+"="+value); element curelement = newdoc.createelement(header); curelement.appendchild(newdoc.createtextnode(value)); rowelement.appendchild(curelement); } } line++; rowscount = line; } filewriter author = null; seek { author = new filewriter(new file(xmlfilename)); transformerfactory tranfactory = transformerfactory.newinstance(); transformer atransformer = tranfactory.newtransformer(); atransformer.setoutputproperty(outputkeys.indent, "yes"); atransformer.setoutputproperty(outputkeys.method, "xml"); atransformer.setoutputproperty("{http://xml.apache.org/xslt}indent-amount", "4"); source src = new domsource(newdoc); streamresult result = new streamresult(writer); atransformer.transform(src, result); writer.flush(); } grab (exception exp) { exp.printstacktrace(); } { seek { writer.close(); } grab (exception e) { } } // output console testing // result result = new streamresult(system.out); } grab (ioexception exp) { system.err.println(exp.tostring()); } grab (exception exp) { system.err.println(exp.tostring()); } system.out.print(rowscount); homecoming rowscount; // "xlm document has been created" + rowscount; } }

java xml csv

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -