[jdom-interest] Grouping based on arbitrary attributes

Jason Long jason at supernovasoftware.com
Tue Mar 11 01:09:31 PST 2003


I am using a funtion similar to the following in one of my projects.  It is
very inefficient.
My problem is that I have no key and have to search the entire document
every time I encounter a new element.
I thought of using xsl key, but it seemed like a hassel with all the
parameters I am matching.
Is there some part of jdom that can help me speed this up using keys or some
other techinque?
I know I can tighten up my xpath so that it does not search every level of
the document.
I have included some code in case my explaination is insufficient.

 public Document groupInventoryOrder(Document doc) throws IOException,
      ServletException {
    JDOMHelper jdh = new JDOMHelper();
    Document docGrouped = null;
    try {
      //load current inventory file
      Document docInventory = jdh.getJDOMFromFile(new File(inventory.xml"));

      //create new inventory Document
      docGrouped = new Document(new Element("inventory"));
      docGrouped.setDocType(new DocType("inventory", "inventory.dtd"));
      Element root = docGrouped.getRootElement();

      String strXPath = "//item";
      XPath xpath = new XPath(strXPath);
      List list = xpath.selectNodes(docInventory);

      for (int i = 0; i < list.size(); i++) {
        Element elt = (Element) list.get(i);
        String id = (String) elt.getAttributeValue("id");
        String size = (String) elt.getAttributeValue("size");
        String weight = (String) elt.getAttributeValue("weight");
        String grade = (String) elt.getAttributeValue("grade");
        String endFin = (String) elt.getAttributeValue("endFin");
        String mill = (String) elt.getChild("mill").getTextTrim();
        String status = (String) elt.getAttributeValue("status");
        String location = (String) elt.getChild("location").getTextTrim();
        float footage = new
Float(elt.getAttributeValue("footage")).floatValue();
        StringBuffer sb = new StringBuffer();

        //set up xpath
        sb.append("//item[@size='" + size + "' and ");
        sb.append("@weight='" + weight + "' and ");
        sb.append("@grade='" + grade + "' and ");
        sb.append("@endFin='" + endFin + "']");

        String xpath = sb.toString();
        XPath xpath2 = new XPath(xpath);

        //use xpath to check for existence of match
        List lst2 = xpath2.selectNodes(docGrouped);
        if (lst2.size() == 0) {
          Element elt22 = new Element("item");
          elt22.setAttribute(new Attribute("id", id));
          elt22.setAttribute(new Attribute("size", size));
          elt22.setAttribute(new Attribute("weight", weight));
          elt22.setAttribute(new Attribute("grade", grade));
          elt22.setAttribute(new Attribute("endFin", endFin));
          elt22.setAttribute(new Attribute("footage", footage + ""));
          elt22.setAttribute(new Attribute("endFin", endFin));
          root.addContent(elt22);
        }
        else if (lst2.size() == 1) {
          Element elt22 = (Element) lst2.get(0);
          float sumFootage = new Float(elt22.getAttributeValue("footage")).
              floatValue();
          elt22.setAttribute(new Attribute("footage", sumFootage + footage +
""));
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
      throw new ServletException("error", e);
    };
    return docGrouped;
  }

Jason Long - CEO and Chief Software Engineer
Supernova Software - supernovasoftware.com
BS Physics, MS  Chemical Engineering





More information about the jdom-interest mailing list