Changeset 36170 for main


Ignore:
Timestamp:
2022-05-10T14:32:16+12:00 (2 years ago)
Author:
kjdon
Message:

added a method for converting a nodelist to string - cos I needed it for debuggin purposes. plus a method for getting named elements based on more than one attribute. eg want all collectionmeta where name='dc.Title' and type='index' - we need to differentiate between index, sort, facet etc colleciton meta

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/util/XMLTools.java

    r36146 r36170  
    158158                if (((Element) child).getAttribute(attribute_name).equals(attribute_value))
    159159                    elements.add((Element) child);
     160            }
     161        }
     162        // not found
     163        if (elements.size() == 0)
     164        {
     165            elements = null;
     166        }
     167        return elements;
     168    }
     169    /**
     170     * returns a list of elements
     171     * parent/node_name[@attribute_name1='attribute_value1', @attribute_name2='attribute_value2']
     172     */
     173    public static ArrayList getNamedElementList(Element parent, String node_name, String[] attribute_names, String[] attribute_values)
     174    {
     175        ArrayList elements = new ArrayList();
     176        NodeList children = parent.getChildNodes();
     177                if (attribute_values.length != attribute_names.length) {
     178                  System.err.println("getNamedElementList - different number of attribute names and values");
     179                  return null;
     180                }
     181        for (int i = 0; i < children.getLength(); i++)
     182        {
     183            //System.out.println("getNamedElementList");
     184            Node child = children.item(i);
     185            //logger.debug("getnamed elem, node nmae="+child.getNodeName());
     186            if (child.getNodeName().equals(node_name))
     187            {
     188                          boolean match = true;
     189                          for (int a=0; a<attribute_names.length; a++) {
     190                            if (!((Element) child).getAttribute(attribute_names[a]).equals(attribute_values[a])) {
     191                              match = false;
     192                              break; // this one is not the right one
     193                            }
     194                          }
     195                          if (match) {
     196                            elements.add((Element) child);
     197                          }
    160198            }
    161199        }
     
    10521090    }
    10531091
     1092  public static String xmlNodeListToString(NodeList nl) {
     1093    StringBuffer sb = new StringBuffer("");
     1094    for (int i=0; i<nl.getLength(); i++) {
     1095      xmlNodeToString(sb, nl.item(i), true, "\t", 2);
     1096    }
     1097    return sb.toString();
     1098  }
    10541099    public static void xmlNodeToString(StringBuffer sb, Node e, boolean indent, String indentString, int depth)
    10551100    {
Note: See TracChangeset for help on using the changeset viewer.