Can anyone write a java code...for converting a text file to xml using xsd.
below is my xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Data">
<xs:complexType>
<xs:sequence>
<xs:element name="Entry" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
and here is my txt file:
John Doe, 30, New York
Jane Smith, 25, Los Angeles
Michael Johnson, 40, Chicago
TXT to XML file converter
Re: TXT to XML file converter
I have implemented following code and it's working:
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.*;
import org.xml.sax.SAXException;
public class TextToXMLConverter {
public static void main(String[] args) {
String inputFilePath = "input.txt";
String outputFilePath = "output.xml";
String xsdFilePath = "schema.xsd";
try {
Document document = createXMLFromText(inputFilePath);
// Save to XML File
saveDocumentToFile(document, outputFilePath);
// Validate using XSD
if (validateXML(outputFilePath, xsdFilePath)) {
System.out.println("XML successfully created and validated against XSD.");
} else {
System.out.println("Validation failed.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static Document createXMLFromText(String filePath) throws IOException, ParserConfigurationException {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
// Root element
Element rootElement = document.createElement("Data");
document.appendChild(rootElement);
String line;
int index = 1;
while ((line = reader.readLine()) != null) {
Element entry = document.createElement("Entry");
entry.setAttribute("id", String.valueOf(index));
entry.setTextContent(line);
rootElement.appendChild(entry);
index++;
}
reader.close();
return document;
}
public static void saveDocumentToFile(Document document, String outputPath) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(outputPath));
transformer.transform(source, result);
System.out.println("XML created at: " + outputPath);
}
public static boolean validateXML(String xmlFilePath, String xsdFilePath) {
try {
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = factory.newSchema(new File(xsdFilePath));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File(xmlFilePath)));
return true;
} catch (SAXException | IOException e) {
System.err.println("Validation error: " + e.getMessage());
return false;
}
}
}
here is the output xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Entry id="1">John Doe, 30, New York</Entry>
<Entry id="2">Jane Smith, 25, Los Angeles</Entry>
<Entry id="3">Michael Johnson, 40, Chicago</Entry>
</Data>
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.*;
import org.xml.sax.SAXException;
public class TextToXMLConverter {
public static void main(String[] args) {
String inputFilePath = "input.txt";
String outputFilePath = "output.xml";
String xsdFilePath = "schema.xsd";
try {
Document document = createXMLFromText(inputFilePath);
// Save to XML File
saveDocumentToFile(document, outputFilePath);
// Validate using XSD
if (validateXML(outputFilePath, xsdFilePath)) {
System.out.println("XML successfully created and validated against XSD.");
} else {
System.out.println("Validation failed.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static Document createXMLFromText(String filePath) throws IOException, ParserConfigurationException {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
// Root element
Element rootElement = document.createElement("Data");
document.appendChild(rootElement);
String line;
int index = 1;
while ((line = reader.readLine()) != null) {
Element entry = document.createElement("Entry");
entry.setAttribute("id", String.valueOf(index));
entry.setTextContent(line);
rootElement.appendChild(entry);
index++;
}
reader.close();
return document;
}
public static void saveDocumentToFile(Document document, String outputPath) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(outputPath));
transformer.transform(source, result);
System.out.println("XML created at: " + outputPath);
}
public static boolean validateXML(String xmlFilePath, String xsdFilePath) {
try {
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = factory.newSchema(new File(xsdFilePath));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File(xmlFilePath)));
return true;
} catch (SAXException | IOException e) {
System.err.println("Validation error: " + e.getMessage());
return false;
}
}
}
here is the output xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Entry id="1">John Doe, 30, New York</Entry>
<Entry id="2">Jane Smith, 25, Los Angeles</Entry>
<Entry id="3">Michael Johnson, 40, Chicago</Entry>
</Data>