xml报文编写以及解析

// 封装电子保单回执报文
    Document document = org.dom4j.DocumentHelper.createDocument();
    document.setXMLEncoding("UTF-8");
    Element root = document.addElement("PACKET");
    Element head = root.addElement("HEAD");
    Element body = root.addElement("BODY");
    root.addAttribute("type" , "RESPONSE");
    root.addAttribute("version" , "1.0");
    Element requestType = head.addElement("REQUEST_TYPE");
    Element subCompany = head.addElement("SUB_COMPANY"); // 主体公司
    Element basePart = body.addElement("BASE_PART");
    Element policyNo = basePart.addElement("POLICY_NO");  // 保单号
    Element receiptDate = basePart.addElement("RECEIPT_DATE"); // 回执日志,指的是当前日期
    // 设置报文内容
    requestType.setText("F01");
    subCompany.setText(cSubCompany);
    policyNo.setText(policyno);
    receiptDate.setText(new Date().toString());
    
    // 调用回执(IDG)
//   doReceiptToIDG(policyno);
    
    // 得到回执报文进行解析
    Document doc = null;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLWriter write = new XMLWriter(out);
    write.write(document);
    SAXReader saxReader = new SAXReader();
    doc = saxReader.read(new ByteArrayInputStream(out.toByteArray()));
    Node backRoot = doc.selectSingleNode("/PACKET ");
    Node backHead = root.selectSingleNode("/HEAD");
    String responseCode = root.selectSingleNode("/RESPONSE_CODE")
    .getText();

你可能感兴趣的:(xml)