org.dom4j.IllegalAddException: No such namespace prefix: *** is in scope on: org.dom4j.tree.DefaultElement (dom4j写入XML文件,标签中带【:】(冒号)解决办法)

时间:2022-11-19 19:23:55

用dom4j操作写XML文件,标签中含有冒号,会报 org.dom4j.IllegalAddException: No such namespace prefix: *** is in scope on: org.dom4j.tree.DefaultElement错误,大致意思就是说,冒号前面的内容是未定义的命名空间,那么我们就帮它定义一下,问题即可解决,看下面例子:

这是我需要生成的XML,标签包含 ofd:

<?xml version="1.0" encoding="UTF-8"?>
<ofd:Signature xmlns:ofd="http://www.ofdspec.org/2016">
<ofd:SignedInfo>
<ofd:Provider ProviderName="Eseal" Company="XXX" Version="1.0"/>
<ofd:SignatureMethod>123456789</ofd:SignatureMethod>
<ofd:SignatureDataTime>988746454656</ofd:SignatureDataTime>
<ofd:Reference FileRef="文件地址">
<ofd:CheckValue>qqqqqqqqqqqqqqqqqqqqq</ofd:CheckValue>
</ofd:Reference>
<ofd:StampAmot PageRef="1" ID="index" Boundary="10.0 20.0 30.0 40.0"/>
<ofd:Seal>
<ofd:BaseLoc>Seal.esl</ofd:BaseLoc>
</ofd:Seal>
</ofd:SignedInfo>
<ofd:SignedValue>SignedValue.dat</ofd:SignedValue>
</ofd:Signature>

代码操作的时候,在根节点用

addNamespace("name","dnsURL")

添加命名空间即可,代码比较简单,不做赘述:

import java.io.FileWriter;
import java.io.IOException; import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter; public class generateXMLTest{ public static void main(String[] args) {
generateSignature("E:\\test.xml");
} public static void generateSignature(String fileAddress) {
FileWriter out = null;
try {
out = new FileWriter(fileAddress);
createDocument().write(out);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(System.out, format);//设置XML编码
writer.write(createDocument());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public static Document createDocument() {
Document document = DocumentHelper.createDocument(); Element root = document.addElement("ofd:Signature")
.addAttribute("xmlns:ofd","http://www.ofdspec.org/2016").addNamespace("ofd", "http://www.ofdspec.org/2016");//在此为ofd:添加命名空间 Element SignedInfo = root.addElement("ofd:SignedInfo"); Element Provider = SignedInfo.addElement("ofd:Provider")
.addAttribute("ProviderName", "Eseal")
.addAttribute("Company", "XXX")
.addAttribute("Version", "1.0"); Element SignatureMethod = SignedInfo.addElement("ofd:SignatureMethod");
SignatureMethod.setText("123456789");
Element SignatureDataTime = SignedInfo.addElement("ofd:SignatureDataTime");
SignatureDataTime.setText("988746454656");
Element Reference = SignedInfo.addElement("ofd:Reference")
.addAttribute("FileRef", "文件地址");
Element CheckValue = Reference.addElement("ofd:CheckValue");
CheckValue.setText("qqqqqqqqqqqqqqqqqqqqq");
Element StampAmot = SignedInfo.addElement("ofd:StampAmot")
.addAttribute("PageRef", "1")
.addAttribute("ID", "index")
.addAttribute("Boundary", "10.0 20.0 30.0 40.0");
Element BaseLoc = SignedInfo.addElement("ofd:Seal").addElement("ofd:BaseLoc");
BaseLoc.setText("Seal.esl");
Element SignedValue = root.addElement("ofd:SignedValue");
SignedValue.setText("SignedValue.dat");
return document;
} }