Java XML Child Element Create addChildEpcList(final Document document, final Element root, final String childEPCs)

Here you can find the source of addChildEpcList(final Document document, final Element root, final String childEPCs)

Description

Adds the EPCs from the given list of childEPCs as elements inside an element to the XML document.

License

Open Source License

Parameter

Parameter Description
document The Document to generate the <epc> and <childEPCs> element from.
root The root Element under which the <childEPCs> element should be generated.
childEPCs A space-separated list of EPCs.

Return

if the element was successfully created, false otherwise.

Declaration

public static boolean addChildEpcList(final Document document, final Element root, final String childEPCs) 

Method Source Code

//package com.java2s;
/*/*from  w ww  .  j  a  va 2s.  c  o  m*/
 * Copyright (C) 2007 ETH Zurich
 *
 * This file is part of Fosstrak (www.fosstrak.org).
 *
 * Fosstrak is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 *
 * Fosstrak is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Fosstrak; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

import java.util.StringTokenizer;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Main {
    /**
     * Adds the EPCs from the given list of childEPCs as {@code <epc>} elements
     * inside an {@code <childEPCs>} element to the XML document.
     * 
     * @param document
     *            The Document to generate the {@code <epc>} and {@code
     *            <childEPCs>} element from.
     * @param root
     *            The root Element under which the {@code <childEPCs>} element
     *            should be generated.
     * @param childEPCs
     *            A space-separated list of EPCs.
     * @return <true> if the {@code <childEPCs>} element was successfully
     *         created, <code>false</code> otherwise.
     */
    public static boolean addChildEpcList(final Document document, final Element root, final String childEPCs) {
        if (isEmpty(childEPCs)) {
            return false;
        }
        Element element = document.createElement("childEPCs");
        StringTokenizer st = new StringTokenizer(childEPCs);
        while (st.hasMoreTokens()) {
            addElement(document, element, st.nextToken(), "epc");
        }
        root.appendChild(element);
        return true;
    }

    private static boolean isEmpty(String s) {
        return s == null || s.trim().equals("");
    }

    private static boolean addElement(final Document document, final Element root, final String elementValue,
            final String elementName) {
        if (isEmpty(elementValue)) {
            return false;
        }
        Element element = document.createElement(elementName);
        element.appendChild(document.createTextNode(elementValue));
        root.appendChild(element);
        return true;
    }
}

Related

  1. addChildElement(Document document, String elementName, Node parentNode)
  2. addChildElement(Element element, String childElementName, Document document)
  3. addChildElementNSElement(Element element, String childElementName, Document document, String nameSpaceUrl)
  4. addChildElementValue(Element element, String childElementName, String childElementValue, Document document)
  5. addChildElementValue(Element element, String childElementName, String childElementValue, Document document)
  6. addChildNode(Document doc, Node parentNode, String childName, String childValue)
  7. addChildText(Document doc, Element parent, String textValue)
  8. addElement(Document doc, Element parent, String nodeName)
  9. addElement(Document doc, Element rootElement, String elementName, String typeIn, String isArrayIn, String partitionerIn)