This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes How to get a  xml id value in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to get a  xml id value in java" Watch "How to get a  xml id value in java" New topic

How to get a xml id value in java

krishna moorthyv

Joined: Dec 13, 2012
Posts: 7
Help me guys:-

i'm doing XML keyword search project

eg:-if i type ton mean

the result like

if i type 200000 mean it will check entire id,if any id contain value mean the the result will come with hireachy..



The below code work fine but the problem is every time i mention a tag value, i don't know how to set a common id value and how to get a id value for the below code,

please any one modify my java code..

<form action="Hserv" method="post">
<input type="text" name="t1" size="34">
<input type="submit" value="search">

<?xml version="1.0" encoding="UTF-8"?>
<staff id="1001">
<staff id="2001">

<staff id="3001">


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String s=request.getParameter("t1");
TestJava k=new TestJava();



package a;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

public class TestJava

public static void TestJava(String s) {

try {

File fXmlFile = new File("E:/xml/xml/src/a/test.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);


System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

NodeList nList = doc.getElementsByTagName("staff");


for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);

System.out.println("\nCurrent Element :" + nNode.getNodeName());

if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;
System.out.println("Staff id : " + eElement.getAttribute("id"));
System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
} catch (Exception e) {

how to set a common id value?please any one modify my code
Paul Clapham

Joined: Oct 14, 2005
Posts: 19747

Well, that looks like code which might be approximately right for the problem you described. But I don't understand what a "common id value" is, and neither do I understand why you would want to set it. Perhaps you could explain?
I agree. Here's the link:
subject: How to get a xml id value in java
jQuery in Action, 3rd edition