Write XML file dom4j operation, the label has a colon will be reported org.dom4j.IllegalAddException: No such namespace prefix: *** is in scope on: org.dom4j.tree.DefaultElement error, roughly meaning that, before the colon content is undefined namespace, then we help it define what the problem can be solved, look at the following example:
This is what I need to generate the XML, tab contains 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>
When the operation code is in the root node
addNamespace("name","dnsURL")
Add a namespace to the code is relatively simple, not repeat them:
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;
}
}