1 package org.apache.archiva.xml;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.w3c.dom.*;
23
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26 import javax.xml.parsers.ParserConfigurationException;
27
28 public class XmlUtil {
29
30
31 public static Document createDocument() throws ParserConfigurationException {
32 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
33 DocumentBuilder builder = factory.newDocumentBuilder();
34 return builder.newDocument();
35 }
36
37 public static Element addChild(Document doc, Element parent, String name) {
38 Element el = doc.createElement(name);
39 parent.appendChild(el);
40 return el;
41 }
42
43 public static Element addChild(Element parent, String name) {
44 Document doc = parent.getOwnerDocument();
45 Element el = doc.createElement(name);
46 parent.appendChild(el);
47 return el;
48 }
49
50 public static String getText(Node element) {
51 if (element!=null) {
52 element.normalize();
53 try {
54 String txt = element.getTextContent();
55 if (txt!=null) {
56 return txt.trim();
57 }
58 } catch (DOMException e) {
59 return "";
60 }
61 }
62 return "";
63 }
64
65 public static Node getChild(Element parent, String name) {
66 NodeList elList = parent.getElementsByTagName(name);
67 if (elList.getLength()>0) {
68 return elList.item(0);
69 } else {
70 return null;
71 }
72 }
73
74 public static String getChildText(Element parent, String name) {
75 return getText(getChild(parent, name));
76 }
77
78 }