英文:
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"
<?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>
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 = "";
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();
}
console output,
Enter the first name: Smith
Full Name : Smith Mike // when enter the first name as "Smith"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论