Java tutorial
//package com.java2s; /** * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved * * The contents of this file are subject to the terms * of the Common Development and Distribution License * (the License). You may not use this file except in * compliance with the License. * * You can obtain a copy of the License at * https://opensso.dev.java.net/public/CDDLv1.0.html or * opensso/legal/CDDLv1.0.txt * See the License for the specific language governing * permission and limitations under the License. * * When distributing Covered Code, include this CDDL * Header Notice in each file and include the License file * at opensso/legal/CDDLv1.0.txt. * If applicable, add the following below the CDDL Header, * with the fields enclosed by brackets [] replaced by * your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * $Id: XMLUtils.java,v 1.15 2009/10/19 18:19:20 asyhuang Exp $ * */ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class Main { private static final Map EMPTY_MAP = Collections.unmodifiableMap(new HashMap()); /** * @param parentNode * is the element tag that contains all the AttirbuteValuePair * tags as children * @return Map Returns the AV pairs in a Map where each entry in the Map is * an AV pair. The key is the attribute name and the value is a Set * of String objects. */ public static Map<String, Set<String>> parseAttributeValuePairTags(Node parentNode) { NodeList keyValueList = parentNode.getChildNodes(); int keyValueSize = keyValueList.getLength(); if (keyValueSize <= 0) { return EMPTY_MAP; } Map<String, Set<String>> resultMap = null; for (int l = 0; l < keyValueSize; l++) { Node keyValueNode = keyValueList.item(l); if (keyValueNode.getNodeType() != Node.ELEMENT_NODE || !keyValueNode.getNodeName().equals("AttributeValuePair")) { continue; } NodeList keyValueEntryList = keyValueNode.getChildNodes(); int keyValueEntrySize = keyValueEntryList.getLength(); if (keyValueEntrySize < 2) { // TODO: More error handling required later for missing // 'Attribute' or 'Value' tags. continue; } Node keyNode = null; // Since Attribute tag is always the first leaf node as per the DTD, and values can one or more, // Attribute tag can be parsed first and then iterate over the values, if any. for (int i = 0; i < keyValueEntrySize; i++) { keyNode = keyValueEntryList.item(i); if (keyNode.getNodeType() == Node.ELEMENT_NODE && keyNode.getNodeName().equals("Attribute")) { break; } } final String key = ((Element) keyNode).getAttribute("name"); Set<String> values = null; // Now parse the Value tags. If there are not 'Value' tags, ignore this key // TODO: More error handling required later for zero 'Value' tags. for (int m = 0; m < keyValueEntrySize; m++) { Node valueNode = keyValueEntryList.item(m); if (valueNode.getNodeType() != Node.ELEMENT_NODE || !valueNode.getNodeName().equals("Value")) { // TODO: Error handling required here continue; } if (values == null) { values = new HashSet<String>(); } Node firstChild = valueNode.getFirstChild(); if (firstChild != null) { String value = firstChild.getNodeValue(); if (value != null) { values.add(value.trim()); } } } if (values == null) { // No 'Value' tags found. So ignore this key. // TODO: More error handling required later for zero // 'Value' tags. continue; } if (resultMap == null) { resultMap = new HashMap<String, Set<String>>(); } Set<String> oldValues = resultMap.get(key); if (oldValues != null) { values.addAll(oldValues); } resultMap.put(key, values); } return resultMap == null ? EMPTY_MAP : resultMap; } public static Set getChildNodes(Node parentNode, String childName) { Set retVal = new HashSet(); NodeList children = parentNode.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (node.getNodeName().equalsIgnoreCase(childName)) { retVal.add(node); } } return (retVal); } }