This project has retired. For details please refer to its Attic page.
XmlUtil xref
View Javadoc
1   package org.apache.archiva.xml;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  }