java - 在创建 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">
解决方案
在 W3C DOM API 中,您需要调用
Document document = domImplementation.createDocument(null, "definition", domImplementation.createDocumentType("definition", "", "L3definitions.dtd"));
DOMImplementation domImplemention = documentBuilder.getDOMImplementation();
您可以在此之前创建一个。
推荐阅读
- php - 在 phpmyadmin 中,我如何将单个数据库中两个不同表中的两个特定行相互链接并保持自动化?
- go - golang中高效的日志解析
- python - 用元组替换列表中的文本
- amazon-web-services - Amazon S3 清单文件是否支持 Parquet 格式?
- html - {% load static %} 在浏览器打开的 html 中显示为文本行
- oracle - 更新超过 1 行 - Oracle SQL 过程
- swift - 我应该怎么做才能在 tableView 中加载异步图像?
- oracle12c - 生成具有固定长度的自动增量ID,包括日期和时间?
- java - 根据业务标准压缩文件夹/文件
- java - 为什么我不能在数组类型 int[][] 上调用此方法