简体   繁体   English

如何正确读取XML文件中的节点值?

[英]How do I properly read a Node Value in an XML file?

I have a problem when I try to retrieve an XML Node Value. 尝试检索XML节点值时遇到问题。 It will be hard to ask this question without a bit of explanation. 没有一点解释就很难问这个问题。 I have a Server - Client Chatroom I have created, and the Server Handles all of the XML Data which contains the User's usernames. 我有一个已经创建的服务器-客户端聊天室,并且服务器处理所有包含用户名的XML数据。 For example the user sends a message that will be broadcasted to everyone on the server. 例如,用户发送一条消息,该消息将广播到服务器上的每个人。 When the message arrives at the server the server will check an array based on the users current IP address. 当消息到达服务器时,服务器将根据用户当前的IP地址检查阵列。 If the IP is in the array another array with a corresponding number will return the username which is then broadcasted to all of the chatroom users. 如果IP在该阵列中,则另一个具有相应编号的阵列将返回用户名,然后将其广播给所有聊天室用户。 My problem surfaces when a new user or an offline user joins the server. 当新用户或脱机用户加入服务器时,我的问题浮出水面。 If the IP is not in the array the server checks an XML file that has all of the online or old users last known IP (This part is getting resolved for the old users who have new IPs) and the server will then add it to the array. 如果IP不在阵列中,服务器将检查一个XML文件,该文件具有所有联机或旧用户的最后一个已知IP(此部分已被拥有新IP的旧用户解决),然后服务器会将其添加到阵列。 This works out in theory however in code it doesn't produce the same results. 从理论上讲这是可行的,但是在代码中它不会产生相同的结果。 I am able to add the Current IP to the array but I can not get the username. 我可以将当​​前IP添加到阵列,但无法获取用户名。 And I believe it is due to the fact that the user's old IP is the node name and the username is the node value. 我认为这是由于用户的旧IP是节点名,而用户名是节点值。 For Example: Usernames.xml File 例如:Usernames.xml文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Server>
    <i0000>User</i0000>
    <i1921681114>Chappy</i1921681114>
</Server>

The function that should add it to the array: 应该将其添加到数组的函数:

public static void addInArray(String IPtoCheck) throws Exception{
        File fXmlFile = new File("Usernames.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        doc.getDocumentElement().normalize();
        NodeList nList = doc.getElementsByTagName(IPtoCheck);
        for(int i=0; nList.getLength()>i; i++){
            Node nNode = nList.item(i);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                if(eElement.getNodeName().equals(IPtoCheck)){
                    int used = 0;
                    while (used < 63){
                        if(IPs[used].equals("no")){
                            IPs[used] = IPtoCheck;
                            usernames[used] = eElement.getNodeValue();
                            AppendArea.Apen(3, usernames[used]);
                            break;
                        }
                        used++;
                    }
                }
            }
        }
    }

Also I should add that i did use 我还要补充一点,我确实使用过

Arrays.fill(IPs, "no");
Arrays.fill(usernames, "no");

no represents an open array spot 否代表一个开放的阵列点

Also the IPtoCheck String is a modified IP value. IPtoCheck字符串也是修改后的IP值。 For example in the XML file the ip node is: 例如,在XML文件中,ip节点为:

i1921681114

this is actually 192.168.1.114 这实际上是192.168.1.114

After all this lengthy explanation my question is as follows: 经过这么长时间的解释,我的问题如下:

How do I properly read a Node Value in an XML file with Java? 如何使用Java正确读取XML文件中的节点值?

because the eElement.getNodeValue(); 因为eElement.getNodeValue(); isn't returning the correct value it only returns 'null' 没有返回正确的值,它仅返回“ null”

The org.w3c.dom.Element object never has a node value, so it will always return null. org.w3c.dom.Element对象永远不会有节点值,因此它将始终返回null。

You need to get its children. 您需要得到它的孩子。 If you use: 如果您使用:

eElement.getFirstChild().getNodeValue();

You will retrieve the first child node of your element, which is the text node you want. 您将检索元素的第一个子节点,即所需的文本节点。

You can also use: 您还可以使用:

eElement.getFirstChild().getTextContent();

which returns the text content of the element, which, in this case, has the same result. 返回元素的文本内容,在这种情况下,元素的文本内容具有相同的结果。

If you find the DOM API in Java counterintuitive, you can use other APIs, such as JDOM or DOM4J, or use other J2SE standard methods such as XPath. 如果发现Java违反直觉的DOM API,则可以使用其他API,例如JDOM或DOM4J,或使用其他J2SE标准方法,例如XPath。

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

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