Reputation: 25
java file code:
public class xmlCreator {
protected DocumentBuilderFactory domFactory = null;
protected DocumentBuilder domBuilder = null;
public xmlCreator() {
try {
domFactory = DocumentBuilderFactory.newInstance();
domBuilder = domFactory.newDocumentBuilder();
} catch (FactoryConfigurationError exp) {
System.err.println(exp.toString());
} catch (Exception exp) {
System.err.println(exp.toString());
}
}
public int convertFile(String csvFileName, String xmlFileName,String delimiter) throws FileNotFoundException {
int rowsCount = -1;
try {
Document newDoc = domBuilder.newDocument();
// Root element
Element rootElement = newDoc.createElement("SynsetDetails");
newDoc.appendChild(rootElement);
// Read csv file
BufferedReader csvReader;
csvReader = new BufferedReader(new FileReader(csvFileName));
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
for (String col : rowValues) {
headers.add(col);
}
} else { // Data row
rowsCount++;
Element rowElement = newDoc.createElement("Synset");
rootElement.appendChild(rowElement);
for (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 = "";
}
Element curElement = newDoc.createElement(header);
curElement.appendChild(newDoc.createTextNode(value));
rowElement.appendChild(curElement);
}
}
line++;
}
ByteArrayOutputStream baos = null;
OutputStreamWriter osw = null;
try {
baos = new ByteArrayOutputStream();
osw = new OutputStreamWriter(baos);
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);
Result result = new StreamResult(osw);
aTransformer.transform(src, result);
//File Creation
//create file
StreamResult file = new StreamResult(new File("C:\\Users\\Nishita\\Desktop\\docs\\synset1.xml"));
//Write data
aTransformer.transform(src, file);
System.out.println("Done");
osw.flush();
System.out.println(new String(baos.toByteArray()));
} catch (Exception exp) {
exp.printStackTrace();
} finally {
try {
osw.close();
} catch (Exception e) {
}
try {
baos.close();
} catch (Exception e) {
}
}
// Output to console for testing
Result result = new StreamResult(System.out);
} catch (IOException exp) {
System.err.println(exp.toString());
} catch (Exception exp) {
System.err.println(exp.toString());
exp.printStackTrace();
}
System.out.println("XLM Document has been created" + rowsCount);
return rowsCount;
}
}
Main Class Code:
public static void main(String[] args) throws FileNotFoundException {
xmlCreator create = new xmlCreator();
create.convertFile("hindi_wordnet_synsets.csv","synsets",";");
}
Csv File:
synset_id;head;"convert(synset using utf8)";"convert(gloss using utf8)";category
1;अजन्मा;अजन्मा,अजात,अनुत्पन्न,अनुद्भूत,अप्रादुर्भूत,अज,अजन,अजन्म,अनन्यभव,अनागत,अयोनि;"जिसनेजन्मनलियाहो";adjective
30168;स्वयंभू;स्वयंभू,स्वयंभु,स्वजन्मा,अयोनिज,आत्मभू,आत्मभव,आत्म-योनि,आत्मसमुद्भव;"जो स्वंय उत्पन्न या पैदा हुआ हो:""भगवान स्वयंभू हैं""";adjective
2;अनासन्न;अशुभ,अमांगलिक,अमाङ्गलिक,अक्षेम,अमंगल,अमङ्गल,अरिष्ट,दग्ध;"जो शुभ न हो:""बिल्ली के द्वारा रास्ता काटा जाना अशुभ माना जाता है""";adjective
3;अप्रविष्ट;अप्रविष्ट;"जो प्रविष्ट न हुआ हो:""अप्रविष्ट अतिथियों को शीघ्र ही भीतर प्रवेश करने दिया जाय""";adjective
4;"चैत्य स्थान";"पवित्र स्थान,चैत्य स्थान,पुण्य भूमि,पुण्य-स्थल,पुण्य स्थल";"वह स्थान जो पवित्र माना जाता हो:""हिंदुओं के लिए काशी एक पवित्र स्थान है""";noun
5;शिवालय;"शिवालय,शिव मंदिर,शिवाला,सिवाला,सौधाल,शिवायतन";"वह मंदिर जिसमें भगवान शिव की मूर्ति स्थापित की गयी हो और वहाँ शिव की आराधना की जाती हो:""वह प्रत्येक सोमवार को शिवालय जाता है""";noun
6;;"अपवित्र स्थान,अपुण्यभूमि,अपवित्रस्थली";"वह स्थान जो पवित्र न हो:""धार्मिक मान्यता है कि भूत-प्रेत अपवित्र स्थानों पर ही निवास करते हैं""";noun
7;;आगत,समागत;"जो आया हुआ हो:""आगत व्यक्तियों का स्वागत करो""";adjective
8;जन्मा;"जन्मा,उत्पन्न,जन्मा हुआ,जात,संजात,जनित,प्रसूत,प्रसून,सूत,रूढ़,अवजनित,पैदा हुआ,संवृत्त,आविर्भूत,उतपन्न";"जो पैदा हुआ हो या जिसने जन्म लिया हो:""जन्मे प्राणियों की मृत्यु निश्चित है / चिंता से जन्मी बीमारियाँ घातक भी हो सकती हैं""";adjective
9;"नैतिक कार्य";"नैतिक कार्य,सत्कर्म,सुकर्म,अच्छा काम,सत्कार्य,सुकृत्य,साधुकर्म,अवदान,धर्म";"ऐसा कार्य जो नीतिपरक हो:""नैतिक कार्यों के द्वारा ही हम समाज का उत्थान कर सकते हैं""";noun
It throws exception:
org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified. org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified.
Please provide solution to this
Upvotes: 1
Views: 278
Reputation: 10383
You use the header names as element names. But the header names contain special characters like brackets and spaces, which are not allowed as XML element names.
Replace all these characters with allowed characters (like underscore or hyphen) in the header names and use the adjusted header strings as element names.
Upvotes: 1