如何使用Java在XML文件中搜索关键字?

huangapple go评论66阅读模式
英文:

How do I search for a keyword in a XML file using Java?

问题

我想使用Java来搜索XML文件中的单词“Smith”,并在控制台中打印“Smith, Mike”。

<?xml version="1.0"?>
<class>
   <student rollno="1">
      <firstname>Doe</firstname>
      <lastname>Jane</lastname>
   </student>
   
   <student rollno="2">
      <firstname>Smith</firstname>
      <lastname>Mike</lastname>
   </student>
   
   <student rollno="3">
      <firstname>Fonda</firstname>
      <lastname>Jane</lastname>
   </student>
</class>

我在网上找到了一些资源来查询整个XML文件,但很难找到在标签内部搜索的方法。

英文:

I would like to use Java to search an XML file for the word "Smith" and print in the console "Smith, Mike"

&lt;?xml version = &quot;1.0&quot;?&gt;
&lt;class&gt;
   &lt;student rollno = &quot;1&quot;&gt;
      &lt;firstname&gt;Doe&lt;/firstname&gt;
      &lt;lastname&gt;Jane&lt;/lastname&gt;
   &lt;/student&gt;
   
   &lt;student rollno = &quot;2&quot;&gt;
      &lt;firstname&gt;Smith&lt;/firstname&gt;
      &lt;lastname&gt;Mike&lt;/lastname&gt;
   &lt;/student&gt;
   
   &lt;student rollno = &quot;3&quot;&gt;
      &lt;firstname&gt;Fonda&lt;/firstname&gt;
      &lt;lastname&gt;Jane&lt;/lastname&gt;
   &lt;/student&gt;
&lt;/class&gt;

I've found resources online to query the whole XML file but had difficulty finding ways to search inside the tags.

答案1

得分: 1

尝试以下解决方案。在这个示例中,使用DOM解析器来解析XML文件。

String firstName = "";
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first name: ");
firstName = scanner.nextLine();

try {
    File inputFile = new File("input.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(inputFile);
    document.getDocumentElement().normalize();
    NodeList nodeList = document.getElementsByTagName("student");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element element = (Element) nodeList.item(i);
        if (element.getElementsByTagName("firstname").item(0).getTextContent().equals(firstName)) {
            System.out.println("Full Name : " + element.getElementsByTagName("firstname").item(0).getTextContent() + " " +
                               element.getElementsByTagName("lastname").item(0).getTextContent());
            break;
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

控制台输出:

Enter the first name: Smith
Full Name : Smith Mike // 输入“Smith”作为姓氏时的输出
英文:

try with following solution. in this example used the DOM Parser for parse XML file.

String firstName = &quot;&quot;;
Scanner scanner = new Scanner(System.in);

System.out.print(&quot;Enter the first name: &quot;);
firstName = scanner.nextLine();
    	
try {
    File inputFile = new File(&quot;input.xml&quot;);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(inputFile);
    document.getDocumentElement().normalize();
    NodeList nodeList = document.getElementsByTagName(&quot;student&quot;);
    for (int i = 0; i &lt; nodeList.getLength(); i++) {
        Element element = (Element) nodeList.item(i);
        if(element.getElementsByTagName(&quot;firstname&quot;).item(0).getTextContent().equals(firstName)){
            System.out.println(&quot;Full Name : &quot;+element.getElementsByTagName(&quot;firstname&quot;).item(0).getTextContent()+&quot; &quot;+
            			   							element.getElementsByTagName(&quot;lastname&quot;).item(0).getTextContent());
            break;
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

console output,

Enter the first name: Smith
Full Name : Smith Mike // when enter the first name as &quot;Smith&quot;

huangapple
  • 本文由 发表于 2020年9月22日 20:29:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64009757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定