Java Can';t使用简单xml解析字符串
我有非常简单的xml,如下所示:Java Can';t使用简单xml解析字符串,java,android,simple-framework,Java,Android,Simple Framework,我有非常简单的xml,如下所示: <EncryptedToken>11d8575638eaa52b</EncryptedToken> String xml = "<root> \n <EncryptedToken>11d8575638eaa52b</EncryptedToken> \n </root>"; } 但是当我试图解析这个xml时,encryptedToken总是null。我的代码有什么问题 更新: 这是我在ma
<EncryptedToken>11d8575638eaa52b</EncryptedToken>
String xml = "<root> \n <EncryptedToken>11d8575638eaa52b</EncryptedToken> \n </root>";
}
但是当我试图解析这个xml时,encryptedToken
总是null
。我的代码有什么问题
更新:这是我在
main
中的示例代码:
Serializer serializer = new Persister();
EncryptedTokenDto encryptedTokenDto = null;
try {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n <EncryptedToken>11d8575638eaa52b</EncryptedToken>";
encryptedTokenDto = serializer.read(EncryptedTokenDto.class, xml);
} catch (Exception e) {
e.printStackTrace();
}
if (encryptedTokenDto != null) {
System.out.println(encryptedTokenDto.getEncryptedToken());
}
一切都好。
所以我的问题是:如何使用SimpleXml解析xml文档而不进行根相关
注意:我需要使用SimpleXml,我不能使用任何其他解析器。尝试@Text,而不是@Element,它将工作,然后尝试@Text,而不是@Element,它将工作,然后尝试使用不同的变量名,您最终会找到它。类似于
encryptedToken
,encryptedToken1
,encryptedToken2
等。问题可能出在返回encryptedToken
,可以尝试将其更改为返回super.encryptedToken
或返回此项。encryptedToken代码>。。检查文档。尝试使用不同的变量名,您最终会找到它。类似于encryptedToken
,encryptedToken1
,encryptedToken2
等。问题可能出在返回encryptedToken
,可以尝试将其更改为返回super.encryptedToken
或返回此项。encryptedToken代码>。。检查文档。
String xml = "<root> \n <EncryptedToken>11d8575638eaa52b</EncryptedToken> \n </root>";
@Root
public class EncryptedTokenDto {
@Element(name = "EncryptedToken", required = false)
private String encryptedToken;
public String getEncryptedToken() {
return this.encryptedToken;
}
public void setEncryptedToken(String encryptedToken) {
this.encryptedToken = encryptedToken;
}
}