Here you can find the source of parseArray(final Node array)
Parameter | Description |
---|---|
dictionary | - XML 'array' element. |
public static List<String> parseArray(final Node array)
//package com.java2s; /**/*from w w w . j a va 2s.c o m*/ * Copyright 2011 Anthony Campbell (anthonycampbell.co.uk) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Utility method to parse a 'array' node of the iTunes meta pList. * * @param dictionary - XML 'array' element. * @return List representing the array element. */ public static List<String> parseArray(final Node array) { // Initialise result final List<String> arrayList = new ArrayList<>(); // Validate if (array != null && array.getNodeType() == Node.ELEMENT_NODE && "array".equals(array.getNodeName())) { final NodeList children = array.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); ++i) { final Node node = children.item(i); if (node != null && node.getNodeType() == Node.ELEMENT_NODE && "dict".equals(node.getNodeName())) { final Map<String, List<String>> dictionary = parseDictionary(node); if (dictionary != null && !dictionary.isEmpty()) { for (final List<String> valueList : dictionary.values()) { if (valueList != null) { arrayList.addAll(valueList); } } } } } } } else { throw new IllegalArgumentException("Provided node is not an array element! (array=" + array + ")"); } return arrayList; } /** * Utility method to parse a 'dict' node of the iTunes meta pList. Updates the * provided iTunes meta data map. * * @param dictionary - XML 'dict' element. * @return Updated iTunes meta data map. */ public static Map<String, List<String>> parseDictionary(final Node dictionary) { // Initialise result final Map<String, List<String>> iTunesMap = new HashMap<>(); // Validate if (dictionary != null && dictionary.getNodeType() == Node.ELEMENT_NODE && "dict".equals(dictionary.getNodeName())) { final NodeList children = dictionary.getChildNodes(); if (children != null) { String key = null; List<String> value = null; for (int i = 0; i < children.getLength(); ++i) { final Node node = children.item(i); if (node != null && node.getNodeType() == Node.ELEMENT_NODE) { switch (node.getNodeName()) { case "key": key = node.getFirstChild().getNodeValue(); break; case "string": value = new ArrayList<>(); value.add(node.getFirstChild().getNodeValue()); break; case "array": value = parseArray(node); break; } } // When ready add new entry if (key != null && value != null) { iTunesMap.put(key, value); key = null; value = null; } } } } else { throw new IllegalArgumentException( "Provided node is not a dictionary element! (dictionary=" + dictionary + ")"); } return iTunesMap; } }