java 解析xml

package test;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.dom4j.Attribute;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.db.DbWrapper;

import utils.FileOperate;

public class XmlTest {
 /**取得系统时间*/
 public static String getDateTime() {
  java.text.SimpleDateFormat f = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  String time = f.format(new java.util.Date());
  return time;
 }
 
 /**记录日志*/
 static void WriteToLog(String message) {
  PrintWriter out = null;
  String strFileName = "";
  String strTime = getDateTime();
  try {
   strFileName = "D:\\ysw\\Log\\Prov_"+strTime.substring(0,10)+".log";
   out = new PrintWriter(new FileOutputStream(strFileName,true));
   out.println(strTime+"   "+message);
   out.close();
  }
  catch(Exception e) {
   out.close();
   e.printStackTrace();
  }
 }
 
 public static void main(String[] args) {
  int nErrorFlag  = 0;
  Element provData;
  String xmlRequest="";
  String result = "";
  try {
//   BufferedReader in = request.getReader();
//   String line ="";
//   while((line = in.readLine())!=null) {
//    xmlRequest=xmlRequest+line+"\n";
//   }
   xmlRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?><MetaReqs>" +
     "<MetaReq xmlns=\"http://www.metadigitsoft.com\" SUBMIT_ID=\"123\" PK_TOTAL=\"aaa\" PK_NUMBER=\"aaa\" REGISTERED_DELIVERY=\"aaa\" MSG_LEVEL=\"aaa\" FEE_USERTYPE=\"aaa\" FEE_TERMINAL_ID=\"aaa\" TP_PID=\"aaa\" TP_UDHI=\"aaa\" MSG_FMT=\"aaa\" MSG_SRC=\"aaa\" FEETYPE=\"aaa\" FEECODE=\"aaa\" VALID_TIME=\"aaa\" AT_TIME=\"aaa\" SRC_ID=\"aaa\" DEST_TERMINAL_ID=\"aaa\" MSG_CONTENT=\"aaa\" LINKID=\"aaa\" UDHI_MSG_ID=\"aaa\" PRIORITY=\"aaa\" MSGID_LD=\"aaa\" SEQ_MOBILELIST=\"aaa\" CH_ID=\"aaa\" SIGN=\"aaa\">" +
     "</MetaReq>" +
     "<MetaReq xmlns=\"http://www.metadigitsoft.com\" SUBMIT_ID=\"456\" PK_TOTAL=\"bbb\" PK_NUMBER=\"aaa\" REGISTERED_DELIVERY=\"aaa\" MSG_LEVEL=\"aaa\" FEE_USERTYPE=\"aaa\" FEE_TERMINAL_ID=\"aaa\" TP_PID=\"aaa\" TP_UDHI=\"aaa\" MSG_FMT=\"aaa\" MSG_SRC=\"aaa\" FEETYPE=\"aaa\" FEECODE=\"aaa\" VALID_TIME=\"aaa\" AT_TIME=\"aaa\" SRC_ID=\"aaa\" DEST_TERMINAL_ID=\"aaa\" MSG_CONTENT=\"aaa\" LINKID=\"aaa\" UDHI_MSG_ID=\"aaa\" PRIORITY=\"aaa\" MSGID_LD=\"aaa\" SEQ_MOBILELIST=\"aaa\" CH_ID=\"aaa\" SIGN=\"aaa\">" +
     "</MetaReq>" +
     "<MetaReq xmlns=\"http://www.metadigitsoft.com\" SUBMIT_ID=\"789\" PK_TOTAL=\"ccc\" PK_NUMBER=\"aaa\" REGISTERED_DELIVERY=\"aaa\" MSG_LEVEL=\"aaa\" FEE_USERTYPE=\"aaa\" FEE_TERMINAL_ID=\"aaa\" TP_PID=\"aaa\" TP_UDHI=\"aaa\" MSG_FMT=\"aaa\" MSG_SRC=\"aaa\" FEETYPE=\"aaa\" FEECODE=\"aaa\" VALID_TIME=\"aaa\" AT_TIME=\"aaa\" SRC_ID=\"aaa\" DEST_TERMINAL_ID=\"aaa\" MSG_CONTENT=\"aaa\" LINKID=\"aaa\" UDHI_MSG_ID=\"aaa\" PRIORITY=\"aaa\" MSGID_LD=\"aaa\" SEQ_MOBILELIST=\"aaa\" CH_ID=\"aaa\" SIGN=\"aaa\">" +
     "</MetaReq>" +
     "</MetaReqs>";
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder db = factory.newDocumentBuilder();
   ByteArrayInputStream stream = new ByteArrayInputStream(xmlRequest.getBytes());
   Document doc = db.parse(stream);
   Element root = doc.getDocumentElement();
   NodeList collegeNodes = root.getChildNodes(); // 获取子节点 
   if (collegeNodes != null) {
    for(int i = 0; i < collegeNodes.getLength(); i++) {
     Node college = collegeNodes.item(i);  
                 if (college != null ) {
                  String SUBMIT_ID = college.getAttributes().getNamedItem("SUBMIT_ID").getNodeValue();
                  String PK_TOTAL = college.getAttributes().getNamedItem("PK_TOTAL").getNodeValue();
                  String PK_NUMBER = college.getAttributes().getNamedItem("PK_NUMBER").getNodeValue();
                  String REGISTERED_DELIVERY = college.getAttributes().getNamedItem("REGISTERED_DELIVERY").getNodeValue();
                  String MSG_LEVEL = college.getAttributes().getNamedItem("MSG_LEVEL").getNodeValue();
                  String FEE_USERTYPE = college.getAttributes().getNamedItem("FEE_USERTYPE").getNodeValue();
                  String FEE_TERMINAL_ID = college.getAttributes().getNamedItem("FEE_TERMINAL_ID").getNodeValue();
                  String TP_PID = college.getAttributes().getNamedItem("TP_PID").getNodeValue();
                  String TP_UDHI = college.getAttributes().getNamedItem("TP_UDHI").getNodeValue();
                  String MSG_FMT = college.getAttributes().getNamedItem("MSG_FMT").getNodeValue();
                  String MSG_SRC = college.getAttributes().getNamedItem("MSG_SRC").getNodeValue();
                  String FEETYPE = college.getAttributes().getNamedItem("FEETYPE").getNodeValue();
                  String FEECODE = college.getAttributes().getNamedItem("FEECODE").getNodeValue();
                  String VALID_TIME = college.getAttributes().getNamedItem("VALID_TIME").getNodeValue();
                  String AT_TIME = college.getAttributes().getNamedItem("AT_TIME").getNodeValue();
                  String SRC_ID = college.getAttributes().getNamedItem("SRC_ID").getNodeValue();
                  String DEST_TERMINAL_ID = college.getAttributes().getNamedItem("DEST_TERMINAL_ID").getNodeValue();
                  String MSG_CONTENT = college.getAttributes().getNamedItem("MSG_CONTENT").getNodeValue();
                  String LINKID = college.getAttributes().getNamedItem("LINKID").getNodeValue();
                  String UDHI_MSG_ID = college.getAttributes().getNamedItem("UDHI_MSG_ID").getNodeValue();
                  String PRIORITY = college.getAttributes().getNamedItem("PRIORITY").getNodeValue();
                  String MSGID_LD = college.getAttributes().getNamedItem("MSGID_LD").getNodeValue();
                  String SEQ_MOBILELIST = college.getAttributes().getNamedItem("SEQ_MOBILELIST").getNodeValue();
                  String CH_ID = college.getAttributes().getNamedItem("CH_ID").getNodeValue();
                  String SIGN = college.getAttributes().getNamedItem("SIGN").getNodeValue();
                  System.err.println(SUBMIT_ID+" "+PK_TOTAL+" "+CH_ID);
                 }
    }
   }   
  } catch (Exception e) {
   
  }
  
//  WriteToLog(xmlRequest);
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值