summaryrefslogtreecommitdiffstats
path: root/pki/base/silent/src/common/ParseXML.java
blob: e80c512d0b61feafcc3a3f7da66721a07fe66eee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

import org.w3c.dom.*;
import org.apache.xml.serialize.*;

import javax.xml.parsers.*;
import java.util.*;
import java.io.*;
import java.net.*;


public class ParseXML {
    Document dom = null;

    public ParseXML() {// nothing
    }

    public void parse(java.io.InputStream is) {
        try {
            // get the factory
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

            // Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            // parse using builder to get DOM representation of the XML file
            dom = db.parse(is);
        } catch (Exception se) {
            System.out.println("ERROR: unable to parse xml");
            se.printStackTrace();

            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();
                String line = null;

                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }

                br.close();
                System.out.println("ERROR XML = " + sb.toString());
            } catch (Exception se1) {
                System.out.println("ERROR: unable to print xml");
                se1.printStackTrace();
            }
        }
    }

    public String getvalue(String tag) {
        String temp = null;

        try {

            // get the root elememt
            Element docEle = dom.getDocumentElement();
				
            // get a nodelist of <employee> elements
            NodeList nl = docEle.getElementsByTagName(tag);

            if (nl != null && nl.getLength() > 0) {
                Element el = (Element) nl.item(0);

                if (el != null) {
                    temp = el.getFirstChild().getNodeValue();
                }
            }
        } catch (Exception e) {
            System.out.println("ERROR: Tag=" + tag + "has no values");
            return null;
        }

        return temp;
    }

    public void prettyprintxml() {
        try {
            // Serialize the document
            OutputFormat format = new OutputFormat(dom);

            format.setLineWidth(65);
            format.setIndenting(true);
            format.setIndent(2);
            XMLSerializer serializer = new XMLSerializer(System.out, format);

            serializer.serialize(dom);
        } catch (Exception e) {}
    }
			
    private String getTextValue(Element ele, String tagName) {
        String textVal = null;
        NodeList nl = ele.getElementsByTagName(tagName);

        if (nl != null && nl.getLength() > 0) {
            Element el = (Element) nl.item(0);

            textVal = el.getFirstChild().getNodeValue();
        }

        return textVal;
    }

    // returns an arraylist of values for the corresponding tag

    public ArrayList constructvaluelist(String first, String second) {
        String temp = null;
        ArrayList al = new ArrayList();

        try {
            // get the root elememt
            Element docEle = dom.getDocumentElement();
		
            // get a nodelist of <employee> elements
            NodeList nl = docEle.getElementsByTagName(first);

            if (nl != null && nl.getLength() > 0) {
                for (int i = 0; i < nl.getLength(); i++) {
                    Element el = (Element) nl.item(i);
                    String value = getTextValue(el, second);

                    System.out.println("tag=" + second + " value=" + value);
                    if (value != null) {
                        al.add(value);
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("ERROR: Tag=" + first + " has no values");
        }

        return al;
    }

    public static void main(String args[]) {
        try {

            ParseXML px = new ParseXML();
            FileInputStream fiscfg = new FileInputStream("/tmp/test.xml");

            px.parse(fiscfg);
            px.prettyprintxml();

        } catch (Exception e) {}
    }

}


; // end class