如何将 XML 文件正确转换为字符串?
How to convet XML file to string properly?
我下面有一个XML签名验证码:
public static boolean isXmlDigitalSignatureValid(String xmlString) throws Exception {
boolean validFlag = false;
String pubicKeyFilePath = "src/main/resources/keys/publickey.key";
Document doc = convertStringToDocument(xmlString);
NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
if (nl.getLength() == 0) {
throw new Exception("No XML Digital Signature Found, document is discarded");
}
PublicKey publicKey = new CryptoUtil().getStoredPublicKey(pubicKeyFilePath);
DOMValidateContext valContext = new DOMValidateContext(publicKey, nl.item(0));
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
XMLSignature signature = fac.unmarshalXMLSignature(valContext);
validFlag = signature.validate(valContext);
return validFlag;
}
但是有个问题。我确定 doc 文件有签名部分,因为我正在打印它。但是
NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
找不到签名部分所以
n1.item(0)
为空,我在 header.
中提到了异常
我明白是怎么回事了。我的 XMLReader 方法工作不正常。是的,它准确地返回字符串,但缺少字节中的一些点。所以我稍微改一下。
首先我阅读并转换为 Doc 文件
public static Document getXmlDocument(String xmlFilePath) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
try {
doc = dbf.newDocumentBuilder().parse(new FileInputStream(xmlFilePath));
} catch (ParserConfigurationException ex) {
ex.printStackTrace();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (SAXException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return doc;
}
然后我将 doc 转换为字符串。
public static Document convertStringToDocument(String xmlStr) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
try
{
builder = factory.newDocumentBuilder();
Document doc = builder.parse( new InputSource( new StringReader( xmlStr ) ) );
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
我最终的 xmlReader 函数是:
public static String xmlReader(String path){
return convertDocumentToString(XmlDigitalSignatureGenerator.getXmlDocument(path));
}
这是我将 XML 文件正确转换为字符串的唯一方法
我下面有一个XML签名验证码:
public static boolean isXmlDigitalSignatureValid(String xmlString) throws Exception {
boolean validFlag = false;
String pubicKeyFilePath = "src/main/resources/keys/publickey.key";
Document doc = convertStringToDocument(xmlString);
NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
if (nl.getLength() == 0) {
throw new Exception("No XML Digital Signature Found, document is discarded");
}
PublicKey publicKey = new CryptoUtil().getStoredPublicKey(pubicKeyFilePath);
DOMValidateContext valContext = new DOMValidateContext(publicKey, nl.item(0));
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
XMLSignature signature = fac.unmarshalXMLSignature(valContext);
validFlag = signature.validate(valContext);
return validFlag;
}
但是有个问题。我确定 doc 文件有签名部分,因为我正在打印它。但是
NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
找不到签名部分所以
n1.item(0)
为空,我在 header.
中提到了异常我明白是怎么回事了。我的 XMLReader 方法工作不正常。是的,它准确地返回字符串,但缺少字节中的一些点。所以我稍微改一下。
首先我阅读并转换为 Doc 文件
public static Document getXmlDocument(String xmlFilePath) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
try {
doc = dbf.newDocumentBuilder().parse(new FileInputStream(xmlFilePath));
} catch (ParserConfigurationException ex) {
ex.printStackTrace();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (SAXException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return doc;
}
然后我将 doc 转换为字符串。
public static Document convertStringToDocument(String xmlStr) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
try
{
builder = factory.newDocumentBuilder();
Document doc = builder.parse( new InputSource( new StringReader( xmlStr ) ) );
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
我最终的 xmlReader 函数是:
public static String xmlReader(String path){
return convertDocumentToString(XmlDigitalSignatureGenerator.getXmlDocument(path));
}
这是我将 XML 文件正确转换为字符串的唯一方法