Updated April 7, 2023
Introduction to XML generator
XML Generator is a tool that can define and generate a massive set of XML Documents based on XSD (Schema Definition). The generated XML file is customized according to user preferences and provides a valid and well-formed XML. The generator takes input as an element that is defined within an XSD. The Data values are defined in the schema.
Syntax
A class containing XML string with an object in CSharp is defined as
Public datatype GetXML(object name)
{
// String variable declaration;
XmlGenerator x=new XmlGenerator();
Xmldoc=();
}
How does the generator work in XML?
XML plays a vital role in modern applications and development. There comes a situation where we need to convert XML Document or XSD through few commercial tools. It is a class file, and for any objects, it generates an XML depends on the property used. For example, in the below section, we have used the Employee class and contains details about the employee. The generated XML by this transformation is used as a base for XML Template. When it doesn’t have proper valid documentation, the generator provides a valid XML document.
Steps to create XML Data from a Schema file
1.First process is to load the XSD file either from the path or directly / Link to access the internet.
2.Press the Generator box to generate XML data.
3. By clicking Options Default value would be changed.
XML Generator has two generators to do so in java.
- DTD Generator Class
- XML schema Generator
XML Schema Class Generator
This creates a Class for the top element provided the global elements with Simple and complex types. A new class Generator is created and inputs the generate () method, which helps in creating an XML document. The Object of the Schema is returned using the getDocType () method.
Using Class for DTD Generator
This takes XML document as input and produces DTD as Output in the Stylus Generator.
Altova XML Spy
XML Spy lets to auto-generate XML, XSD and supports DTD to XSD and JSON conversions.
Stylus Studio
This Stylus Studio XML generates well-formed and Valid XML as it produces well-organized Schema associations and allows to valid against Schema definitions or any validating XML Parser. The depth of the generated XML nodes is even predicted, which helps in setting the child nodes.
XML Generator – Informatica
This generator is very helpful in generating XML Files inside a Pipeline Concept or from the Database table. The First Step is to create a Record Table like
USE [ Source]
GO
SELECT [ EID] ,[Ename] ,[LName],[Designation] FROM Employee
Well, we are going to generate an XML File Using SQL DB.
The created table is shown in SQL.
The next process is importing XML Definition, which creates a mapping that drags and drops Record Source from the Source Folder. The Local XSD file is selected from the options.
Similarly, we can generate sample XML files using Oxygen XML editor where the tab includes URL, Namespace, and other import and export Settings.
Examples
In this section, we shall see how XML and DTD documents are processed and validated in Informatica as well as Stylus Studio to produce a Well-formed document.
Example #1
Code:
import java.util.ArrayList;
import java.util.List;
public class XMLSample extends Base {
protected Mapping map = null;
protected Source s = null;
protected Target t = null;
protected void create() {
s = createOracleJobSource("O_Source");
folder.addSource(s);
}
protected void createTargets() {
t = this.Relational(s.Oracle, "Ora_t");
}
protected void createMap() throws Exception {
mapping = new Mapping("XMLParserGeneratorTransformation", "XMLParserGeneratorTransformation", "Mapping for XMLParserGeneratorTransformation");
setMapFileName(mapping);
TransformHelper help = new TransformHelper(mapping);
RowSet rs = (RowSet) helper.sourceQualifier(this.s).getRowSets().get(0);
OutputSet of = (OutputSet) helper.xmlParser(rs, "XML_Parser_Transformation","amples\\employee.xsd");
RowSet or = (RowSet) of.getRowSets().get(0);
OutputSet oGen = (OutputSet) helper.xmlGenerator(outputTarget, "XML_Generator_Transformation", "amples\\employee.xsd");
RowSet Target = (RowSet) outGen.getRowSets(). get(0);
mapping.writeTarget(Target, this.t);
folder.addMapping(mapping);
}
protected void createSession() throws Exception {
sessionm = new Sessionms ("Job for C Active", "Work for C", "This is work process of Session");
session.setMapping(mapping);
}
protected void createWorkflow() throws Exception {
workflow = new Workflows("Workf", "Workf", "This flow process of Custom Transformation");
workflow.addSession(session);
folder.addWorkFlow(workflow);
}
protected Source createOracleJobSource(String dbN) {
Source jo = null;
List<Fi> fields = new ArrayList<Fi>();
Field jobID = new Field ("JOB_ID", "JOB_ID", "", NativeDataTypes.Oracle. VARCHAR2, "12", "0", FieldKeyType.PRIMARY_KEY, FieldType.jo, true);
fields.add(jobID);
ConnectionInfo in = getRelationalConnInfo(SourceTargetType.Oracle, dbN);
jobSource = new Source("JOBS", "JOBS", "This is employee table", "JOBS", in);
jobSource.setFields(fields);
return jobSource;
}
public static void main(String[] args) {
try {
XMLParserGeneratorSample cus = new XMLParserGeneratorSample();
if (args.length > 0) {
if (cus.validateRunMode(args[0])) {
cus.execute();
}
} else {
customActive.printUsage();
}
} catch (Exception e1) {
e1.printStackTrace();
System.err.println("Exception created is: " + e1.getMessage());
}
}
}
Explanation:
The above code parses an XML document in making a connection with the data source by making mapping transformation. And parsing the values in the database.
Output:
Example #2
XML DTD
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bookdb [
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title, author+, category*, language?, year?, edition?, price)>
<!ATTLIST book ISBN CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT category (#PCDATA)>
<!ELEMENT language (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT edition (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>
<bookdb>
<book ISBN="03001">
<title>Java For Dummies</title>
<author>Tan Ah Teck</author>
<category>Programming</category>
<year>2001</year>
<edition>7</edition>
<price>19.99</price>
</book>
<book ISBN="6002">
<title>More Java For Dummies</title>
<author>Tan Ah Teck</author>
<category>Programming</category>
<year>2001</year>
<price>25.99</price>
</book>
<book ISBN="012">
<title>The Complete Guide to Fishing</title>
<author>Bill Jones</author>
<author>James Cook</author>
<author>Mary Turing</author>
<category>Fishing</category>
<category>Leisure</category>
<language>French</language>
<year>2020</year>
<edition>2</edition>
<price>49.99</price>
</book>
</bookdb>
Explanation
This is the generated XML document which is validated using Stylus Studio. The output shows the Well-formed Document.
Output:
Features
- Has an ability to import XML document Structure either manually or by XSD.
- Defines and combines an XML structure into a single Output format.
- Good Comfortness feature for Project design. Offers a powerful data Generation engine for data Creation.
- Has Good Flexibility in customizing the XML document by handling elements groups and repeating elements.
Conclusion – XML generator
Coming to the end of this article, we have enjoyed learning the generation of XML documents and also understood their usages and concepts. This tool is powerful in Accelerating XML development by allowing existing XML Schema to efficiently generate instance documents.
Recommended Articles
This is a guide to the XML generator. Here we discuss how the generator works in XML and the Steps to create XML Data from a Schema file. You may also have a look at the following articles to learn more –