如何使用Java获取XML id值?

时间:2013-01-30 19:50:26

标签: java xml servlets

对于XML关键字搜索项目,用户可以搜索XML代码中的任何值:

<?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>

如果用户搜索20000,则结果将在ID 10012001中提供,因此输出应为:

  ms 
  dhoni
   msd
  200000

  yuraj
 singh
  yuvi
 20000

我想用Java做到这一点。这是我到目前为止所写的内容:

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

}

2 个答案:

答案 0 :(得分:1)

这看起来像是XPath查询的工作!

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

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    // This XPath query will give you a list of only those <staff> elements
    // which contain a <salary>200000</salary> element
    XPathExpression expr = xpath.compile("/company/staff/salary[text()=\"200000\"]/..");
    NodeList result = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);

    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        Element eElement = (Element) nodes.item(i);
        String id = eElement.getAttribute("id");
        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());
    }

答案 1 :(得分:0)

这可能不是最好和最短的方法,但你可以这样做: -

  1. 创建一个Player类并将firstname,lastname,nickname,salary设置为变量并生成getter / setter。
  2. 读取xml文件并使用Jaxb将xml转换为对象。一个很好的link开始
  3. 现在使用您现在在这些对象中搜索的对象
    
    object.getActive().compareTo(salary)//compare the player object salary with desires salary in a loop
    
  4. 我希望这有帮助!