首页 > 解决方案 > 在创建 xml 转换器期间添加自定义行

问题描述

我需要创建一个基于接收到的 TelegramInfo 对象创建 xml 的类。我唯一缺少的是在 XML 声明后向我的文件添加自定义行

这是我的代码:

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;
    }
}

我的 XML 输出:

<?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>

我正在寻找的结果:

<?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>

任何人都可以帮助我使用变压器添加额外的线路吗?

<!DOCTYPE DEFINITIONS SYSTEM "L3definitions.dtd">

标签: javaxml

解决方案


在 W3C DOM API 中,您需要调用

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

DOMImplementation domImplemention = documentBuilder.getDOMImplementation();您可以在此之前创建一个。


推荐阅读