0

one quick question: How can I make this function shorter or more performant:

private String getOCC_Name(Document doc) {
    String occ_name = "";
    NodeList nl = doc.getElementsByTagName("Occurrence");
    for (int x = 0; x < nl.getLength(); x++) {
        Element e = (Element) nl.item(x);
        if (checkId(e, this.getPrimaryOccurenceRef().getId() /* = id7*/)) {
            String instancedRef = e.getAttributes().getNamedItem("instancedRef").getNodeValue().replace("#", "");
            NodeList pr = doc.getElementsByTagName("ProductRevision");
            for (int p = 0; p < pr.getLength(); p++) {
                Element pre = (Element) pr.item(p);
                if (checkId(pre, instancedRef)) {
                    String DatasetId = pre.getElementsByTagName("AssociatedDataSet").item(0).getAttributes().getNamedItem("dataSetRef").getNodeValue().replace("#", "");
                    NodeList ds = doc.getElementsByTagName("DataSet");
                    for (int dsi = 0; dsi < ds.getLength(); dsi++) {
                        if (checkId((Element) ds.item(dsi), DatasetId)) {
                            String[] memberRefs = ds.item(dsi).getAttributes().getNamedItem("memberRefs").getNodeValue().replace("#", "").split(" ");
                            NodeList efs = doc.getElementsByTagName("ExternalFile");
                            for (int efl = 0; efl < efs.getLength(); efl++) {
                                for (String m : memberRefs) {
                                    if (checkId((Element) efs.item(efl), m) && efs.item(efl).getAttributes().getNamedItem("format").getNodeValue().equalsIgnoreCase("asm")) {
                                        occ_name = efs.item(efl).getAttributes().getNamedItem("locationRef").getNodeValue().split("\\\\")[1];
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return occ_name;
}
private Boolean checkId(Element e, String toCheck) {
    if (e.getAttributes().getNamedItem("id").getNodeValue().equalsIgnoreCase(toCheck)) {
        return true;
    }
    return false;
}

I need to go the following way to get my wanted String:

primaryOccurrence (with reference to "id7") -> instancedRefs ->

Productrevision (with reference of instancedRefs) -> associatedDataSet ->

Dataset (with reference of associatedDataSet) -> memberRefs ->

ExternalFile (with reference to memberRefs) -> locationRef

EDIT:

My question has nothing to do with the marked duplicate...

IVIike
  • 87
  • 8

1 Answers1

0

Q: how to make this function shorter / more performant?

A: Use XPath queries to find the node you're looking for, rather than iterating through the whole tree:

Here are a couple of tutorials:

paulsm4
  • 114,292
  • 17
  • 138
  • 190