Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java XML检查节点是否存在_Java_Xml - Fatal编程技术网

Java XML检查节点是否存在

Java XML检查节点是否存在,java,xml,Java,Xml,我对Java和XML有问题。 我有一个注册表,它将新用户保存在一个文件users.xml中,在保存当前用户之前,我想检查是否存在具有相同用户名的其他用户 这是我的XML: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <user id="1"> <username>John</username> <password>mypassword</password> &

我对Java和XML有问题。 我有一个注册表,它将新用户保存在一个文件users.xml中,在保存当前用户之前,我想检查是否存在具有相同用户名的其他用户

这是我的XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<user id="1">
<username>John</username>
<password>mypassword</password>
</user>
但每次我使用:

if(isUserExisting.checkIfExists(username)) {
System.out.println("This username exists");
}
我收到以下错误:

[Fatal Error] users.xml:6:2: The markup of the document that follow the element must have a correct format. 
org.xml.sax.SAXParseException; systemId: file:/eclipse/workspace/Folder/src/users.xml; lineNumber: 6; columnNumber: 2; The markup of the document that follow the element must have a correct format. 

有什么问题吗?提前感谢:)

对我来说运行正常。我没有收到
致命错误

要使其正常工作,您需要使用
equals()
方法正确比较字符串: 替换


XML中只能有一个根元素,因此如果您想要有多个用户,则需要一个外部元素。与错误无关,但请查看为什么不使用单个XPath表达式进行检查?
[Fatal Error] users.xml:6:2: The markup of the document that follow the element must have a correct format. 
org.xml.sax.SAXParseException; systemId: file:/eclipse/workspace/Folder/src/users.xml; lineNumber: 6; columnNumber: 2; The markup of the document that follow the element must have a correct format. 
if(tempUser == username)
if (tempUser.equals(username))