Java XML检查节点是否存在
我对Java和XML有问题。 我有一个注册表,它将新用户保存在一个文件users.xml中,在保存当前用户之前,我想检查是否存在具有相同用户名的其他用户 这是我的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> &
<?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))