use of javax.xml.parsers.DocumentBuilder in project weixin-java-tools by chanjarster.
the class WxCryptUtilTest method testNormal.
public void testNormal() throws ParserConfigurationException, SAXException, IOException {
WxCryptUtil pc = new WxCryptUtil(token, encodingAesKey, appId);
String encryptedXml = pc.encrypt(replyMsg);
System.out.println(encryptedXml);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(new InputSource(new StringReader(encryptedXml)));
Element root = document.getDocumentElement();
String cipherText = root.getElementsByTagName("Encrypt").item(0).getTextContent();
String msgSignature = root.getElementsByTagName("MsgSignature").item(0).getTextContent();
String timestamp = root.getElementsByTagName("TimeStamp").item(0).getTextContent();
String nonce = root.getElementsByTagName("Nonce").item(0).getTextContent();
String messageText = String.format(xmlFormat, cipherText);
// 第三方收到企业号平台发送的消息
String plainMessage = pc.decrypt(cipherText);
System.out.println(plainMessage);
assertEquals(plainMessage, replyMsg);
}
use of javax.xml.parsers.DocumentBuilder in project weixin-java-tools by chanjarster.
the class WxCryptUtilTest method testValidateSignatureError.
public void testValidateSignatureError() throws ParserConfigurationException, SAXException, IOException {
try {
WxCryptUtil pc = new WxCryptUtil(token, encodingAesKey, appId);
String afterEncrpt = pc.encrypt(replyMsg);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
StringReader sr = new StringReader(afterEncrpt);
InputSource is = new InputSource(sr);
Document document = db.parse(is);
Element root = document.getDocumentElement();
NodeList nodelist1 = root.getElementsByTagName("Encrypt");
String encrypt = nodelist1.item(0).getTextContent();
String fromXML = String.format(xmlFormat, encrypt);
// 这里签名错误
pc.decrypt("12345", timestamp, nonce, fromXML);
} catch (RuntimeException e) {
assertEquals(e.getMessage(), "加密消息签名校验失败");
return;
}
fail("错误流程不抛出异常???");
}
use of javax.xml.parsers.DocumentBuilder in project weixin-java-tools by chanjarster.
the class WxCryptUtil method extractEncryptPart.
static String extractEncryptPart(String xml) {
try {
DocumentBuilder db = builderLocal.get();
Document document = db.parse(new InputSource(new StringReader(xml)));
Element root = document.getDocumentElement();
return root.getElementsByTagName("Encrypt").item(0).getTextContent();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of javax.xml.parsers.DocumentBuilder in project cw-omnibus by commonsguy.
the class WeatherFragment method buildForecasts.
private ArrayList<Forecast> buildForecasts(String raw) throws Exception {
ArrayList<Forecast> forecasts = new ArrayList<Forecast>();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(raw)));
NodeList times = doc.getElementsByTagName("start-valid-time");
for (int i = 0; i < times.getLength(); i++) {
Element time = (Element) times.item(i);
Forecast forecast = new Forecast();
forecasts.add(forecast);
forecast.setTime(time.getFirstChild().getNodeValue());
}
NodeList temps = doc.getElementsByTagName("value");
for (int i = 0; i < temps.getLength(); i++) {
Element temp = (Element) temps.item(i);
Forecast forecast = forecasts.get(i);
forecast.setTemp(Integer.valueOf(temp.getFirstChild().getNodeValue()));
}
NodeList icons = doc.getElementsByTagName("icon-link");
for (int i = 0; i < icons.getLength(); i++) {
Element icon = (Element) icons.item(i);
Forecast forecast = forecasts.get(i);
forecast.setIcon(icon.getFirstChild().getNodeValue());
}
return (forecasts);
}
use of javax.xml.parsers.DocumentBuilder in project cw-android by commonsguy.
the class StaticFileDemo method onCreate.
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
selection = (TextView) findViewById(R.id.selection);
try {
InputStream in = getResources().openRawResource(R.raw.words);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in, null);
NodeList words = doc.getElementsByTagName("word");
for (int i = 0; i < words.getLength(); i++) {
items.add(((Element) words.item(i)).getAttribute("value"));
}
in.close();
} catch (Throwable t) {
Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
}
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
}
Aggregations