Search in sources :

Example 46 with DocumentBuilder

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);
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 47 with DocumentBuilder

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("错误流程不抛出异常???");
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 48 with DocumentBuilder

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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 49 with DocumentBuilder

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);
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 50 with DocumentBuilder

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));
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document)

Aggregations

DocumentBuilder (javax.xml.parsers.DocumentBuilder)883 Document (org.w3c.dom.Document)694 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)622 Element (org.w3c.dom.Element)339 IOException (java.io.IOException)276 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)276 NodeList (org.w3c.dom.NodeList)267 InputSource (org.xml.sax.InputSource)238 SAXException (org.xml.sax.SAXException)235 Node (org.w3c.dom.Node)199 StringReader (java.io.StringReader)167 Test (org.junit.Test)127 DOMSource (javax.xml.transform.dom.DOMSource)102 File (java.io.File)99 ByteArrayInputStream (java.io.ByteArrayInputStream)86 InputStream (java.io.InputStream)73 ArrayList (java.util.ArrayList)72 StreamResult (javax.xml.transform.stream.StreamResult)65 Transformer (javax.xml.transform.Transformer)59 SAXParseException (org.xml.sax.SAXParseException)56