简体   繁体   English

用Java解析XML文件

[英]Parse XML File with Java

I have an XML file that I am trying to search using Java. 我有一个要使用Java搜索的XML文件。 I need to find an element by its attribut value (Port number) and return the depending description of the element. 我需要通过其属性值(端口号)找到一个元素,并返回该元素的相关描述。

The xml file of known ports should be hosted online and has an architecture like: 已知端口的xml文件应在线托管,并具有以下架构:

<ports>
    <record>
        <number></number>
        <name></name>
        <protocol></protocol>
        <description></description>
    </record>
    <record>
        <number></number>
        <name></name>
        <protocol></protocol>
        <description></description>
    </record>
    <record>
        <number></number>
        <name></name>
        <protocol></protocol>
        <description></description>
    </record>
    <record>
        <number></number>
        <name></name>
        <protocol></protocol>
        <description></description>
    </record>
</ports>

The elements have no unique identifier. 元素没有唯一标识符。 In my application i want to call a function with a number as parameter and it shoult give me the description of the item with the given attribute "number". 在我的应用程序中,我想调用一个带有数字作为参数的函数,它应该给我有关具有给定属性“数字”的项目的描述。

My problem is, its a list of all known ports and i cannot manually edit the structure to assign all elements with the portnumber as attribute. 我的问题是,它包含所有已知端口的列表,而我无法手动编辑结构以将所有端口号分配为属性。 can anyone show me how to solve that? 谁能告诉我如何解决这个问题?

thanks in advance 提前致谢

UPDATE: i want to search it like get_port(int portno) . 更新:我想像get_port(int portno)一样搜索它。 thats my code : 多数民众赞成在我的代码

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DOMExampleJava {

    public static void main(String args[]) {

        try {

            File input = new File("input.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(input);
            doc.getDocumentElement().normalize();

            NodeList nodes = doc.getElementsByTagName("record");
            System.out.println("==========================");

                for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);

                    if (node.getNodeType() == Node.ELEMENT_NODE) {

                        Element element = (Element) node;
                        System.out.println("Port number: " + getValue("number", element));
                        System.out.println("Protocol: " + getValue("protocol", element));
                        System.out.println("Description: " + getValue("description", element));

                    }

                }

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    }

    private static String getValue(String tag, Element element) {

        NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
        Node node = (Node) nodes.item(0);
        return node.getNodeValue();

    }

}

Create your own HashMap obj inside the method and you can get the record you want from the list. 在方法内创建自己的HashMap obj,然后可以从列表中获取所需的记录。 Eg HashMap<int itemId, List<item>> yourOwnItem = new HashMap<>(); 例如HashMap<int itemId, List<item>> yourOwnItem = new HashMap<>(); At the end of the for loop pass the item to yourOwItem as follows: 在for循环结束时,将项目传递给yourOwItem,如下所示:
yourOwnItem.put(i , List<item>);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM