473,287 Members | 3,228 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

hello, im new to java. i hav a code in which is it should not print duplicate values

but it is displaying al values der in xml file.it should avoid reading duplicate values ,pls help me..
my code is,

Expand|Select|Wrap|Line Numbers
  1. public class nm {
  2. public static void main(String argv[]) {
  3.  
  4.   try {
  5.   File file = new File("c://adv.xml");
  6.   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  7.   DocumentBuilder db = dbf.newDocumentBuilder();
  8.   Document doc = db.parse(file);
  9.   doc.getDocumentElement().normalize();
  10.   System.out.println("Root element " + doc.getDocumentElement().getNodeName());
  11.   NodeList nodeLst = doc.getElementsByTagName("Z12");
  12.   for (int s = 0; s < nodeLst.getLength(); s++) {
  13.  
  14.         Node fstNode = nodeLst.item(s);
  15.  
  16.         if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
  17.  
  18.                Element fstElmnt = (Element) fstNode;
  19.           NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("Value");
  20.           Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
  21.           NodeList fstNm = fstNmElmnt.getChildNodes();
  22.           //System.out.println("value : "  + ((Node) fstNm.item(0)).getNodeValue());
  23.           NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("DisplayName");
  24.           Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
  25.           NodeList lstNm = lstNmElmnt.getChildNodes();
  26.           String zf=((Node) lstNm.item(0)).getNodeValue();
  27.  
  28.               System.out.println("Diaplay Name : " + zf);
  29.  
  30.         }
  31.  
  32.       }
  33.       } catch (Exception e) {
  34.         e.printStackTrace();
  35.       }
  36.      }
  37.     } 
this is my code,
ouput is,
Root element Search_Config
value : BC
Diaplay Name : Bearing complete
value : BC01
Diaplay Name : Bearing complete 01
value : BC
Diaplay Name : Bearing complete
value : BC01
Diaplay Name : Bearing complete 01

but here i want bc, bc01 shoud print only once not twice.
Nov 16 '08 #1
1 1308
JosAH
11,448 Expert 8TB
For every element with index i in your list check whether it is also in the list at
position [0,i-1]. If it is, don't print it because you have seen it in the list before.

Hint: make a separate, private, little (boolean) method to search for the element.

kind regards,

Jos
Nov 16 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
3
by: Markus Dehmann | last post by:
I have a two different value types with which I want to do similar things: store them in the same vector, stack, etc. Also, I want an << operator for each of them. class Value{}; // this would...
6
by: Rhino | last post by:
I am writing Java UDFs using DB2 V8.2 (Fixpack 8) on Windows XP. I would like to create some common code classes that are visible to the UDFs on my system but I'm not having a lot of luck so far....
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
26
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I...
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.