0

Im new to java. This is my first java code using parser, i was able to check the node value of particular tag by

if(name != null && name.equals("Length"))

how to pass multiple values in name value like name.equals("Length","Length1","Length3")), i have to check around 2000 values

below is partial code

      if (file.exists()) { 
          Document doc = db.parse(file); 
          Element docEle = doc.getDocumentElement(); 

          NodeList Line = docEle.getElementsByTagName("Lineno");
          System.out.println("size " + Line.getLength());

          if(Line != null && Line.getLength() > 0)
          {                      
            if(doc1.getDocumentElement() == null)
            {
                Element root1 = doc1.createElement("Line");    
                doc1.appendChild(root1);   
            }
            for(int j = 0 ;j < nodeValue.getLength();j++)
            {
               Element el = (org.w3c.dom.Element) nodeValue.item(j);
               String name = el.getAttribute("name");
               String value = el.getAttribute("void");
               String valueFromNode = el.getTextContent();
               if(name != null && name.equals("id"))
               {
                  ITEMID =valueFromNode; 
               }
               if(name != null && name.equals("Length"))
               {
                //  System.out.println("This has datcode " + ele);
                  datecode = "Yes";
                  cwi = valueFromNode;
                //  doc1.getDocumentElement().appendChild(doc1.importNode(imported, true));    
                }
             }
        }
    }
2
  • 1
    You can put all the values in a list, iterate throught the list and check if you have found a value :) Commented Jun 9, 2014 at 14:38
  • 1
    you could perhaps use a "while" loop.. Commented Jun 9, 2014 at 14:39

3 Answers 3

1

You can do this by filling your lengths values into ArrayList, then ask if the name contains that array.

For Example:

ArrayList<String> lengths = new ArrayList<String>();
//add all lengths values into array

if(lengths.contains(name){ 
    //do something
}
Sign up to request clarification or add additional context in comments.

Comments

1

Put all the values in an ArrayList and use .contains():

List<String> strings = new ArrayList<>();
strings.add("Length");

And then:

if (name != null && strings.contains(name))

Comments

0

If I would need to compare single String with large collection for it existance in it, i would use HashSet<String>. Insteed of testing equality, I would check if my String object is in my set. If it does - than it is equal.

HashSet<String> compSet=new HashSet<String>();
//put strings to comparision
if(name!=null && compSet.contains(name)){
//hey it was equal.
}

3 Comments

The best way to lern how to swim? Throw onto deep water :)
Also.. Nothing is mentioned about the elements being unique.. Assumptions are dangerous :)
But what is the difference if my target is equal with second or 10th element of collection - guess that these elements were the same;]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.