• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get a xml id value in java

 
krishna moorthyv
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help me guys:-

i'm doing XML keyword search project

eg:-if i type ton mean

the result like
sachin
tendulkar
ton

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

MS
Dhoni
MSD
200000

yuraj
singh
yuvi
200000

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">
</form>

<?xml version="1.0" encoding="UTF-8"?>
<company>
<staff id="1001">
<firstname>sachin</firstname>
<lastname>tendulkar</lastname>
<nickname>TON</nickname>
<salary>100000</salary>
</staff>
<staff id="2001">
<firstname>MS</firstname>
<lastname>Dhoni</lastname>
<nickname>MSD</nickname>
<salary>200000</salary>
</staff>

<staff id="3001">
<firstname>yuraj</firstname>
<lastname>singh</lastname>
<nickname>yuvi</nickname>
<salary>200000</salary>
</staff>

</company>

Hserv.java

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

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

System.out.println(s);

}

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;
import java.io.File;

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);


doc.getDocumentElement().normalize();

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

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

System.out.println("----------------------------");

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;
if(eElement.getElementsByTagName("salary").item(0).getTextContent().equals(s))
{
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) {
e.printStackTrace();
}
}

}
how to set a common id value?please any one modify my code
 
Paul Clapham
Sheriff
Pie
Posts: 20166
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic