接收POST数据,解析XML

package org.eclipsebook.ch09;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.Element;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;
import org.dom4j.Attribute;
import org.dom4j.xpath.*;
/*
import org.dom4j.io.XMLWriter;
*/
import java.util.List;
import java.util.Iterator;
import java.io.*;
import javax.xml.parsers.*;
import javax.servlet.*;
import javax.servlet.http.*;

//org.apache.catalina.logger.FileLogger
public class RevXml extends HttpServlet
{
 public void doGet(HttpServletRequest request,
         HttpServletResponse response )
   throws IOException , ServletException 
 {
  doPost(request,response);
 }
 public void doPost(HttpServletRequest request,
   HttpServletResponse response )
   throws IOException , ServletException 
 {
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();

  String XmlContent = null;
  try {
   XmlContent = getDoc(request);
   XmlContent = XmlContent.replace("xmlns=\"http://www.monternet.com/dsmp/schemas/\"","");
   Document document = DocumentHelper.parseText(XmlContent);
   
//   List childNodes   =   document.selectNodes("//SOAP-ENV:Envelope/SOAP-ENV:Header");

   Node nodeHead = document.selectSingleNode("//SOAP-ENV:Envelope/SOAP-ENV:Header/TransactionID");
   out.println(nodeHead.getText());
   out.println("ok");
   Node nodeBody = document.selectSingleNode("//SOAP-ENV:Envelope/SOAP-ENV:Body/SyncOrderRelationReq/Send_Address/DeviceID");
   if ( nodeBody != null )                               
        out.println(nodeBody.getText());
   
   Element root = document.getRootElement();
   for ( Iterator i = root.elementIterator(); i.hasNext(); ) 
   { 
        Element element = ( Element ) i.next();
        out.println(element.getPath());
           out.println("<br>");
           for ( int j = 0; j < element.nodeCount(); j++ )
           {
           Node node = element.node(j);
                
           if (node instanceof Element)
           {
                out.print(node.getUniquePath());
                out.print(j);
                out.print("--");
                out.print(node.getText());
                out.print("--");
                out.println(node.getName());
                out.println("<br>");
           }
          }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
// 获取post数据
 public Document getPostDoc(HttpServletRequest request) 
  throws IOException, DocumentException
 {
  SAXReader xmlReader = new SAXReader();
  InputStream in;
  Document document = null;
  
  in = request.getInputStream();
  document = xmlReader.read(in);
  return document;
 }
 public String getDoc(HttpServletRequest request) 
   throws Exception 
 {
  char[] readerBuffer = new char[request.getContentLength()];
  BufferedReader bufferedReader = request.getReader();
  
  //Logger.info("开始处理上传数据");
  int portion = bufferedReader.read(readerBuffer);
  int amount = portion;
  while (amount < readerBuffer.length) 
  {
   portion = bufferedReader.read(readerBuffer, 
           amount, readerBuffer.length - amount);
   amount = amount + portion;
  }

  StringBuffer stringBuffer = new StringBuffer(
                  (int) (readerBuffer.length * 1.5) );
  for (int index = 0; index < readerBuffer.length; index++) 
  {
   char c = readerBuffer[index];
   stringBuffer.append(c);
  }
 
  String xml = stringBuffer.toString();
  //logger.info(xml);
  return xml;
 } 
}

你可能感兴趣的:(接收POST数据,解析XML)