1,通过SOCKET发送HTTP请求,得到服务器的响应,服务器返回一个包含HTTP信息,如何获得如Content-Length,Content-Type等,这些HTTP头的信息(需要取得各种信息的值)。2,服务器返回一个XML信息,格式如下:
<root>
<head>
<no1>hello!</no1>
<no2>welcome!</no2>
</head>
<body>
<list>thisisvalue</list>
<list>这个是可以循环的多个接点</list>
</body>
</root>如何读取每个接点的值(list部分是循环的)。得到
hello!
welcome!thisisvalue
这个是可以循环的多个接点请问:以上功能如何实现?

解决方案 »

  1.   

    (1)java.lang.String getHeader(java.lang.String name) 
              Returns the value of the specified request header as a String. 
    (2)java.util.Enumeration getHeaderNames() 
              Returns an enumeration of all the header names this request contains. 
    (3)java.util.Enumeration getHeaders(java.lang.String name) 
              Returns all the values of the specified request header as an Enumeration of String objects 
    通过上面的方法,你可以得到Http请求头部信息,这些是HttpServletRequest的方法。
    要获得xml中的结点值可以通过sax或dom解析xml文件获得!
      

  2.   

    用DOM具体是怎么取得各接点的值啊
      

  3.   

    用java的话还是用jdom比较舒服
      

  4.   

    自己去做才有体会,你需要http头的一些知识,socket(服务端)相关的东东,还有xml解析...这些对于一个软件开发人员并不难
      

  5.   

    解析
    import javax.xml.parsers.*;
    import org.w3c.dom.*;public class DOMCheck {
        int i = 0;
        public static Document getDocument() throws Exception {
            return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("student.xml");
        }
        
        public static void readXml() throws Exception {
      
            Document doc = getDocument();
            String name = doc.getFirstChild().getNodeName();
            //System.out.println(doc.getElementsByTagName("name1").item(0).getNodeName());
            NodeList nodP = doc.getElementsByTagName(name);
            int len = nodP.getLength();
            for (int i = 0; i < len; i++) {
               read(nodP.item(i));
            }
            
            
        }
        
        public static void read(Node nod) {
            if (nod instanceof Element) {
                if (nod.hasChildNodes()) {
                    NodeList nodC  = nod.getChildNodes();
                    int len  = nodC.getLength();
                    if (len == 1) {
                        System.out.println(((Element) nod).getTagName() + ", " + nod.getTextContent());
                    }
                    for (int i = 1; i < len; i++) {
                        read(nodC.item(i));
                    }
                } 
                
            }
            
        }    
        public void mm() {
            i=0;
        }
        /**
         * @param args
         */
        public static void main(String[] args) {
            try {
                readXml();
            } catch (Exception e) {
                e.printStackTrace();
            }    }}
      

  6.   

    写入XML
    import javax.xml.parsers.*;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import org.w3c.dom.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.*;
    import javax.xml.transform.stream.*;public class DOMWrite {
        
        public static Document getDocument() throws Exception {
            return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            
        }
        public static void write() throws Exception {
            Document doc = getDocument();
            File fout = new File("student1.xml");
            FileOutputStream fos = new FileOutputStream(fout);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            Element elpp = doc.createElement("Students");
            Element elp = doc.createElement("student");
            elp.setAttribute("id", "1001");
           
            Element elc = doc.createElement("name");
            elc.appendChild(doc.createTextNode("kender"));
            elp.appendChild(elc);
            elpp.appendChild(elp);
            doc.appendChild(elpp);
            //TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(fout));
            TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(bos));
            
            
        }    /**
         * @param args
         * @throws Exception 
         */
        public static void main(String[] args) throws Exception {
                write();
             }}