1package org.apache.archiva.xml;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.w3c.dom.*;
2324import javax.xml.parsers.DocumentBuilder;
25import javax.xml.parsers.DocumentBuilderFactory;
26import javax.xml.parsers.ParserConfigurationException;
2728publicclassXmlUtil {
293031publicstatic Document createDocument() throws ParserConfigurationException {
32 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
33 DocumentBuilder builder = factory.newDocumentBuilder();
34return builder.newDocument();
35 }
3637publicstatic Element addChild(Document doc, Element parent, String name) {
38 Element el = doc.createElement(name);
39 parent.appendChild(el);
40return el;
41 }
4243publicstatic Element addChild(Element parent, String name) {
44 Document doc = parent.getOwnerDocument();
45 Element el = doc.createElement(name);
46 parent.appendChild(el);
47return el;
48 }
4950publicstatic String getText(Node element) {
51if (element!=null) {
52 element.normalize();
53try {
54 String txt = element.getTextContent();
55if (txt!=null) {
56return txt.trim();
57 }
58 } catch (DOMException e) {
59return"";
60 }
61 }
62return"";
63 }
6465publicstatic Node getChild(Element parent, String name) {
66 NodeList elList = parent.getElementsByTagName(name);
67if (elList.getLength()>0) {
68return elList.item(0);
69 } else {
70returnnull;
71 }
72 }
7374publicstatic String getChildText(Element parent, String name) {
75return getText(getChild(parent, name));
76 }
7778 }