com.ewcms.util.XMLUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.ewcms.util.XMLUtil.java

Source

/**
 * Copyright (c)2010-2011 Enterprise Website Content Management System(EWCMS), All rights reserved.
 * EWCMS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * http://www.ewcms.com
 */
package com.ewcms.util;

import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * dom4j?XML
 * 
 * @author wuzhijun
 */
public class XMLUtil {
    private Document document = null;

    public Document getDocument() {
        return document;
    }

    /**
     * ?Document
     */
    public XMLUtil() {
        document = DocumentHelper.createDocument();
    }

    /**
     * ?
     * 
     * @param rootName
     * @return
     */
    public Element addRoot(String rootName) {
        Element root = document.addElement(rootName);
        return root;
    }

    /**
     * ?
     * 
     * @param parentElement
     * @param elementName
     * @return
     */
    public Element addNode(Element parentElement, String elementName) {
        Element node = parentElement.addElement(elementName);
        return node;
    }

    /**
     * 
     * 
     * @param thisElement
     * @param attributeName
     * @param attributeValue
     */
    public void addAttribute(Element thisElement, String attributeName, String attributeValue) {
        thisElement.addAttribute(attributeName, attributeValue);
    }

    /**
     * 
     * 
     * @param thisElement
     * @param attributeNames
     * @param attributeValues
     */
    public void addAttributes(Element thisElement, String[] attributeNames, String[] attributeValues) {
        for (int i = 0; i < attributeNames.length; i++) {
            thisElement.addAttribute(attributeNames[i], attributeValues[i]);
        }
    }

    /**
     * 
     * 
     * @param thisElement
     * @param text
     */
    public void addText(Element thisElement, String text) {
        thisElement.addText(text);
    }

    /**
     * ?XML
     * 
     * @return
     * @throws IOException
     */
    public String getXML() {
        return document.asXML().substring(39);
    }
}