项目

一般

简介

XMLDisposal.java

XML分析模块 - 唐 辉丰, 2011-05-16 23:28

下载 (2.74 KB)

 
1
package MyBot;
2

    
3
import javax.xml.parsers.*;
4
import java.io.*;
5
import org.w3c.dom.*;
6
import org.xml.sax.*;
7

    
8
/** 
9
 * @author  tanghf
10
 * @version 1.3
11
 * @since 2011/5/10
12
 **/ 
13

    
14
public class XMLDisposal {
15
        
16
        public XMLDisposal() throws IOException{
17
                
18
        }
19
        public static void main(String[] args) {
20

    
21
        }
22
        
23
        public static String GetType(String args){
24
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
25
                try{
26
                        DocumentBuilder builder = factory.newDocumentBuilder();
27
                        Document doc = builder.parse(new InputSource(new StringReader(args)));
28
                        doc.normalize();
29
                        Node type = doc.getElementsByTagName("type").item(0).getFirstChild();
30
                        String typevalue = type.getNodeValue();
31
                        return typevalue;
32
                }catch(Exception e){
33
                        e.printStackTrace();
34
                        return null;
35
                }
36
        }
37
        
38
        public static String GetUser(String args){
39
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
40
                try{
41
                        DocumentBuilder builder = factory.newDocumentBuilder();
42
                        Document doc = builder.parse(new InputSource(new StringReader(args)));
43
                        doc.normalize();
44
                        Node user = doc.getElementsByTagName("user").item(0).getFirstChild();
45
                        String uservalue = user.getNodeValue();
46
                        return uservalue;
47
                }catch(Exception e){
48
                        e.printStackTrace();
49
                        return null;
50
                }
51
        }
52
        
53
        public static String GetTime(String args){
54
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
55
                try{
56
                        DocumentBuilder builder = factory.newDocumentBuilder();
57
                        Document doc = builder.parse(new InputSource(new StringReader(args)));
58
                        doc.normalize();
59
                        Node time = doc.getElementsByTagName("time").item(0).getFirstChild();
60
                        String timevalue = time.getNodeValue();
61
                        return timevalue;
62
                }catch(Exception e){
63
                        e.printStackTrace();
64
                        return null;
65
                }
66
        }
67
        
68
        public static String GetContent(String args){
69
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
70
                try{
71
                        DocumentBuilder builder = factory.newDocumentBuilder();
72
                        Document doc = builder.parse(new InputSource(new StringReader(args)));
73
                        doc.normalize();
74
                        Node content = doc.getElementsByTagName("content").item(0).getFirstChild();
75
                        String contentvalue = content.getNodeValue();
76
                        return contentvalue;
77
                }catch(Exception e){
78
                        e.printStackTrace();
79
                        return null;
80
                }
81
        }
82
        
83
        public static String XmlBuild(String type, String time, String user, String content){
84
                
85
                StringBuffer sb = new StringBuffer();
86
                sb.append("<?xml version=\"1.0\" encoding=\"GB2312\" ?>\n");
87
                sb.append("<msg>\n");
88
                sb.append("\t<type>"+type+"</type>\n");
89
                sb.append("\t<user>"+user+"</user>\n");
90
                sb.append("\t<time>"+time+"</time>\n");
91
                sb.append("\t<content>"+content+"</content>\n");
92
                sb.append("</msg>");
93
                return sb.toString();
94
        }
95

    
96
}