Home > OS >  Add custom line during xml creation Transformer
Add custom line during xml creation Transformer

Time:10-06

I need to make a class which will create xml based on received TelegramInfo object. The only thing which i am missing is adding custom line to my file after XML declaration

Here is my code:

public class ConverterToDbStyle implements Convertable {

    @Override
    public String convert(TelegramInfo telegramInfo) {
        try {       
            DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder;
            documentBuilder = documentFactory.newDocumentBuilder();
            Document document = documentBuilder.newDocument();
 
            //Root element
            Element root = document.createElement("definition");
            document.appendChild(root);
            
            // Table node
            Element table = document.createElement("table");
            Attr attr1 = document.createAttribute("name");
            attr1.setTextContent(telegramInfo.getName());
            Attr attr2 = document.createAttribute("record");
            attr2.setTextContent("");
            table.setAttributeNode(attr1);
            root.appendChild(table);
            
            for(Field field : telegramInfo.getFields()) {
                Element item = document.createElement("item");
                Attr name = document.createAttribute("name");
                name.setTextContent(field.getName());
                Attr l3Type = document.createAttribute("L3Type");
                l3Type.setTextContent(field.getL3Type());
                item.setAttributeNode(name);
                item.setAttributeNode(l3Type);
                table.appendChild(item);
            }

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
            
            DOMSource domSource = new DOMSource(document);
            StreamResult streamResult = new StreamResult(new File("./data/out/test.xml")); 
            
            transformer.transform(domSource, streamResult);
            
        } catch (ParserConfigurationException | TransformerException e) {
            e.printStackTrace();
        }
        return null;
    }
}

My XML output:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<definition>
    <table name="TABLE_NAME">
        <item L3Type="string" name="field1"/>
        <item L3Type="string" name="field2"/>
        <item L3Type="number" name="field3"/>
        <item L3Type="number" name="field4"/>
        <item L3Type="number" name="field5"/>
        <item L3Type="string" name="field6"/>
        <item L3Type="number" name="field7"/>
        <item L3Type="date" name="date"/>
    </table>
</definition>

The result which I am looking for:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE DEFINITIONS SYSTEM "L3definitions.dtd">
<definition>
    <table name="TABLE_NAME">
        <item L3Type="string" name="field1"/>
        <item L3Type="string" name="field2"/>
        <item L3Type="number" name="field3"/>
        <item L3Type="number" name="field4"/>
        <item L3Type="number" name="field5"/>
        <item L3Type="string" name="field6"/>
        <item L3Type="number" name="field7"/>
        <item L3Type="date" name="date"/>
    </table>
</definition>

Can anyone help me with adding that additional line using transformer?

<!DOCTYPE DEFINITIONS SYSTEM "L3definitions.dtd">

CodePudding user response:

In the W3C DOM API, you need to call

Document document = domImplementation.createDocument(null, "definition", domImplementation.createDocumentType("definition", "", "L3definitions.dtd"));  

You can create a DOMImplementation domImplemention = documentBuilder.getDOMImplementation(); before that.

CodePudding user response:

This is doing the job, but thank you for your time

        DOMImplementation domImpl = document.getImplementation();
        DocumentType doctype = domImpl.createDocumentType("doctype", "TABLES", "L3definitions_db.dtd");
  • Related