我正在使用PeopleSoft系统调用的Java类对包含特殊字符(非ASCII字符,如ã,á,à,ç等)的XML文档进行签名。为避免出现任何问题,我删除了这些字符,但实际上我需要使用它们打印此签名文档。有什么办法吗?这是要签名的XML文档的示例(已准备好进行签名):
<PedidoEnvioLoteRPS xmlns="http://www.prefeitura.sp.gov.br/nfe" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Cabecalho Versao="1" xmlns="">
<CPFCNPJRemetente>
<CNPJ>99999999999999</CNPJ>
</CPFCNPJRemetente>
<transacao>false</transacao>
<dtInicio>2018-10-02</dtInicio>
<dtFim>2018-10-02</dtFim>
<QtdRPS>1</QtdRPS>
<ValorTotalServicos>40</ValorTotalServicos>
<ValorTotalDeducoes>0</ValorTotalDeducoes>
</Cabecalho>
<RPS xmlns="">
<Assinatura>FavH23VVIbPWzlvJ28OZZ26Lv2aEgWfmsdhPN1qQN19UCxv6xzu8fHC50wnji3i3G49DuYoXy354U2IxzooPtZYWv7KFUwWLWC4xJYpNKNLOg3txx4znxDNbdC9l/ot9liIMKHf/8rJdciGMpwUOMxt3z95sFVJDcvx/3si1yQG0TaQsWLLLKHH4rUwfE+OWYBIwp/CWBf1/IRzYsFb/q2UgpvfvU1RaXIgI+aNqwYyKulhfUZItI4nYJTsGcXG0y+iXxW3oRWiCGJ5leOysHyJ4VLJcg/vehwT8f8ZQLhvClKeDQUQpL9ts+9oX4PHdc8WXDgN5ekUmvCHS/GW0ew==</Assinatura>
<ChaveRPS>
<InscricaoPrestador>99999999</InscricaoPrestador>
<SerieRPS>1</SerieRPS>
<NumeroRPS>180</NumeroRPS>
</ChaveRPS>
<TipoRPS>RPS</TipoRPS>
<DataEmissao>2018-10-02</DataEmissao>
<StatusRPS>N</StatusRPS>
<TributacaoRPS>T</TributacaoRPS>
<ValorServicos>40</ValorServicos>
<ValorDeducoes>0</ValorDeducoes>
<CodigoServico>3205</CodigoServico>
<AliquotaServicos>2</AliquotaServicos>
<ISSRetido>false</ISSRetido>
<CPFCNPJTomador>
<CNPJ>88888888888888</CNPJ>
</CPFCNPJTomador>
<RazaoSocialTomador>XPTO S.A.</RazaoSocialTomador>
<EnderecoTomador>
<Logradouro>Av do Lago</Logradouro>
<NumeroEndereco>999</NumeroEndereco>
<ComplementoEndereco>9 andar - cj. 99</ComplementoEndereco>
<Bairro>Vila Guilherme</Bairro>
<Cidade>3505708</Cidade>
<UF>SP</UF>
<CEP>99999999</CEP>
</EnderecoTomador>
<EmailTomador>teste@teste.com.br</EmailTomador>
<Discriminacao>Tarifa de antecipação de entrega VR Saúde Familiar: R$ 40,00||||||||||||||||IRRF 1,5% Sob Responsabilidade de VR Benefícios Serv Proc Ltda conforme I.N. 153/87 e |Lei 7450/85, art. 53 - R$ 0,60|Trib aprox. Lei nº 12.741/12: R$5,38 Federal, R$1,68 Municipal e R$32,94 pelos serviços|Fonte:IBPT/empresometro.com.br A3S28F 18.2.B|Contrato XPTO|Autorização de Regime especial - SEI 6017.2018/0055420-5 (32600,94)|REALIZE O PAGAMENTO APENAS DE BOLETOS EMITIDOS POR VOCÊ NA ÁREA LOGADA E SEGURA DO SEU|PORTAL RH. PREVINA-SE E EVITE PREJUÍZOS FINANCEIROS.</Discriminacao>
<ValorCargaTributaria>7.06</ValorCargaTributaria>
<PercentualCargaTributaria>17.64</PercentualCargaTributaria>
<FonteCargaTributaria>IBPT</FonteCargaTributaria>
</RPS>
</PedidoEnvioLoteRPS>
我用来签名的java方法是:
public void AssinaXML(String ArqAssinar) {
try {
/* Creates the DOM document DOM from the file in ArqAssinar */
DocumentBuilderFactory DocBuilderFactory = DocumentBuilderFactory.newInstance();
DocBuilderFactory.setNamespaceAware(true);
DocumentBuilder DocBuilder = DocBuilderFactory.newDocumentBuilder();
FileInputStream Input = new FileInputStream(ArqAssinar);
Document Doc = DocBuilder.parse(Input);
/* Gets the position of the Signature tag */
Node Tag = Doc.getDocumentElement();
if (Tag != null) {
/* Signs the document */
DOMSignContext DocSignCont = new DOMSignContext(PrivPass, Tag);
XMLSignature Signature = XmlSignFac.newXMLSignature(SignInfo, KeyInf);
Signature.sign(DocSignCont);
/* Creates the Signature tag with the results */
OutputStream Saida = new FileOutputStream(ArqAssinar);
TransformerFactory TransformFac = TransformerFactory.newInstance();
Transformer Transf = TransformFac.newTransformer();
Transf.transform(new DOMSource(Doc), new StreamResult(Saida));
}
else {
System.out.println("Java Assinatura_Digital, método AssinaXML - A tag especificada para inserir a assinatura não foi encontrada");
}
}
catch (Exception E) {
E.PrintStackTrace();
}
}
但是当我尝试在上面的XML文档上签名时出现此错误:
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:3字节UTF-8序列的无效字节2。 在com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(未知来源) com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(未知来源) 在com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(未知来源) 在com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar(未知来源) 在com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next(未知来源) 在com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(未知来源) 在com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(未知来源) 在com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(未知来源) 在com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(未知来源) 在com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(未知来源) 在com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(未知来源) 在com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(未知来源) 在com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(未知来源) 在javax.xml.parsers.DocumentBuilder.parse(未知来源) 在GVR_Assinatura_Digital.AssinaXML(GVR_Assinatura_Digital.java:551) 在GVR_Assinatura_Digital.main(GVR_Assinatura_Digital.java:778)
有人知道为什么会这样吗?
答案 0 :(得分:1)
正如评论者所说,您可以签署任何有效的XML文档。
真正的问题应该是,如何创建一个包含ã等字符的有效XML文档?
答案:
UTF-8编码
<test>Ã¥</test>
如果您在创建包含非ASCII字符的有效XML文档时遇到问题,请创建一个新的Stack Overflow问题,其中包括正在(或应该)正在创建XML文件的代码段。
另请参阅:
答案 1 :(得分:0)
我找到了解决方案:我刚刚进行了交易:
FileInputStream Input = new FileInputStream(ArqAssinar);
Document Doc = DocBuilder.parse(Input);
为此:
InputStream Input = new FileInputStream(ArqAssinar);
Reader Leitor = new InputStreamReader(Input, "UTF-8");
InputSource Origem = new InputSource(Leitor);
Document Doc = DocBuilder.parse(Origem);