org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR

你。 提交于 2019-12-07 07:41:45

问题


I've been looking way too long at this and can't figure out what I'm doing wrong.

So, I'm trying to generate a Xades signature for some content. Unfortunately I always run into the same error: "HIERARCHY_REQUEST_ERR". This is my XML document:

<?xml version="1.0" encoding="UTF-8"?>
<object>
    <request id="f9e1294a-64b7-488b-b475-7511e317e399">(some arbitrary base64 encoded content)</request>
</object>

I'm trying to sign the "Request" element (obviously...), with the following code:

/*create a document*/
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element objectElement = doc.createElement("object");
doc.appendChild(objectElement);
Element requestElement = doc.createElement("request");
requestElement.appendChild(doc.createTextNode(decodedContent));
requestElement.setAttribute("id", UUID.randomUUID().toString());
objectElement.appendChild(requestElement);

/*Key provider, signing profile & signer itself*/
KeyingDataProvider kp = new CustomKeyingDataProvider(certificate, privateKey);
XadesSigningProfile p = new XadesTSigningProfile(kp);
XadesSigner signer = p.newSigner();

/*Signed data*/
DataObjectDesc flatFile = new DataObjectReference("#" + requestElement.getAttribute("id"))
    .withTransform(new GenericAlgorithm("http://www.w3.org/2000/09/xmldsig#base64"))
    .withDataObjectTimeStamp();
SignedDataObjects dataObjs = new SignedDataObjects(flatFile).withCommitmentType(AllDataObjsCommitmentTypeProperty.proofOfOrigin());

/*Actual signing*/
signer.sign(dataObjs, doc);

I get this error in return (abbreviated to what was necessary):

class org.w3c.dom.DOMException: org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An    
attempt was made to insert a node where it is not permitted. 
    at org.apache.xerces.dom.CoreDocumentImpl.insertBefore(Unknown Source)
    at org.apache.xerces.dom.NodeImpl.appendChild(Unknown Source)
    at xades4j.production.AppendAsLastChildStrategy.append(SignatureAppendingStrategies.java:55)
    at xades4j.production.SignerBES.sign(SignerBES.java:210)
    at xades4j.production.SignerBES.sign(SignerBES.java:122)
    ...

I searched the web, but the only similar error I found was this one: https://code.google.com/p/xades4j/wiki/QeA (almost on top). I can't really find an answer to his question, but as far as I can see, my XML document is with a root element (just like his second example). So I don't really know what I'm doing wrong...

Is there anyone able to help me? Thanks in advance.


回答1:


You're trying to append the signature as the root element of the document, since you're supplying doc as the parent on the sign method. However, there's already a root element on the document (object), and only one is alowed.



来源:https://stackoverflow.com/questions/23520982/org-w3c-dom-domexception-hierarchy-request-err

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!