This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.xml;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.w3c.dom.*;
023
024import javax.xml.parsers.DocumentBuilder;
025import javax.xml.parsers.DocumentBuilderFactory;
026import javax.xml.parsers.ParserConfigurationException;
027
028public class XmlUtil {
029
030
031    public static Document createDocument() throws ParserConfigurationException {
032        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
033        DocumentBuilder builder = factory.newDocumentBuilder();
034        return builder.newDocument();
035    }
036
037    public static Element addChild(Document doc, Element parent, String name) {
038        Element el = doc.createElement(name);
039        parent.appendChild(el);
040        return el;
041    }
042
043    public static Element addChild(Element parent, String name) {
044        Document doc = parent.getOwnerDocument();
045        Element el = doc.createElement(name);
046        parent.appendChild(el);
047        return el;
048    }
049
050    public static String getText(Node element) {
051        if (element!=null) {
052            element.normalize();
053            try {
054                String txt = element.getTextContent();
055                if (txt!=null) {
056                    return txt.trim();
057                }
058            } catch (DOMException e) {
059                return "";
060            }
061        }
062        return "";
063    }
064
065    public static Node getChild(Element parent, String name) {
066        NodeList elList = parent.getElementsByTagName(name);
067        if (elList.getLength()>0) {
068            return elList.item(0);
069        } else {
070            return null;
071        }
072    }
073
074    public static String getChildText(Element parent, String name) {
075        return getText(getChild(parent, name));
076    }
077
078}