Java org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23520982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:32:01  来源:igfitidea点击:

org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR

javaxades4j

提问by testuser

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:

因此,我正在尝试为某些内容生成 Xades 签名。不幸的是,我总是遇到同样的错误:“HIERARCHY_REQUEST_ERR”。这是我的 XML 文档:

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

我搜索了网络,但发现的唯一类似错误是:https: //code.google.com/p/xades4j/wiki/QeA(几乎在顶部)。我真的找不到他的问题的答案,但据我所知,我的 XML 文档有一个根元素(就像他的第二个例子)。所以我真的不知道我做错了什么......

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

有没有人可以帮助我?提前致谢。

采纳答案by lgoncalves

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

您正在尝试将签名附加为文档的根元素,因为您是doc作为sign方法的父元素提供的。但是,文档 ( object)上已经有一个根元素,并且只允许一个。