source: trunk/gli/src/org/greenstone/gatherer/util/XMLTools.java@ 7979

Last change on this file since 7979 was 7979, checked in by mdewsnip, 20 years ago

A utility class containing useful XML processing functions.

  • Property svn:keywords set to Author Date Id Revision
File size: 3.1 KB
Line 
1package org.greenstone.gatherer.util;
2
3
4import java.io.*;
5import java.util.*;
6import org.apache.xerces.parsers.*;
7import org.apache.xml.serialize.*;
8import org.w3c.dom.*;
9import org.xml.sax.*;
10
11
12/** This class is a static class containing useful XML functions */
13public class XMLTools
14{
15 static public ArrayList getChildElementsByTagName(Element parent_element, String element_name)
16 {
17 ArrayList child_elements = new ArrayList();
18
19 NodeList children_nodelist = parent_element.getChildNodes();
20 for (int i = 0; i < children_nodelist.getLength(); i++) {
21 Node child_node = children_nodelist.item(i);
22 if (child_node.getNodeType() == Node.ELEMENT_NODE && child_node.getNodeName().equals(element_name)) {
23 child_elements.add(child_node);
24 }
25 }
26
27 return child_elements;
28 }
29
30
31 static public String getElementTextValue(Element element)
32 {
33 // Find the first text node child
34 NodeList children_nodelist = element.getChildNodes();
35 for (int i = 0; i < children_nodelist.getLength(); i++) {
36 Node child_node = children_nodelist.item(i);
37 if (child_node.getNodeType() == Node.TEXT_NODE) {
38 return child_node.getNodeValue();
39 }
40 }
41
42 // None found
43 return null;
44 }
45
46
47 /** Parse an XML document from a given file */
48 static public Document parseXMLFile(File xml_file)
49 {
50 Document document = null;
51
52 try {
53 FileInputStream fis = new FileInputStream(xml_file);
54 InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
55 Reader r = new BufferedReader(isr);
56 InputSource isc = new InputSource(r);
57 DOMParser parser = new DOMParser();
58 parser.setFeature("http://xml.org/sax/features/validation", false);
59 parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
60 // May or may not be ignored, the documentation for Xerces is contradictory. If it works then parsing -should- be faster.
61 parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
62 parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
63 parser.parse(isc);
64 document = parser.getDocument();
65 isr.close();
66 fis.close();
67 }
68 catch (Exception exception) {
69 exception.printStackTrace();
70 }
71
72 return document;
73 }
74
75
76 /** Write an XML document to a given file */
77 static public void writeXMLFile(File xml_file, Document document)
78 {
79 try {
80 OutputStream os = new FileOutputStream(xml_file);
81 // Create an output format for our document.
82 OutputFormat f = new OutputFormat(document);
83 f.setEncoding("UTF-8");
84 f.setIndenting(true);
85 f.setLineWidth(0); // Why isn't this working!
86 f.setPreserveSpace(false);
87 // Create the necessary writer stream for serialization.
88 OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
89 Writer w = new BufferedWriter(osw);
90 // Generate a new serializer from the above.
91 XMLSerializer s = new XMLSerializer(w, f);
92 s.asDOMSerializer();
93 // Finally serialize the document to file.
94 s.serialize(document);
95 // And close.
96 os.close();
97 }
98 catch (Exception exception) {
99 exception.printStackTrace();
100 }
101 }
102}
103
Note: See TracBrowser for help on using the repository browser.