如何使用 Java 获取 XML id 值? [英] How to get an XML id value using Java?

查看:23
本文介绍了如何使用 Java 获取 XML id 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For an XML keyword search project, users may search any value in the XML code:

<?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 中可用,因此输出应如下所示:

If the user searches for 20000, the result will available in ID 1001 and 2001 so the output should be like:

  ms 
  dhoni
   msd
  200000

  yuraj
 singh
  yuvi
 20000

我想用 Java 来做这件事.以下是我到目前为止所写的内容:

I would like to do this in Java. Here is what I've written so far:

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

}

推荐答案

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

This looks like a job for XPath queries!

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

这篇关于如何使用 Java 获取 XML id 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆