[英]Can't parse string with simple xml
I have pretty simple xml like this: 我有这样的简单XML:
<EncryptedToken>11d8575638eaa52b</EncryptedToken>
Here is my java class for it: 这是我的Java类:
@Root
public class EncryptedTokenDto {
@Element(name = "EncryptedToken", required = false)
private String encryptedToken;
public String getEncryptedToken() {
return encryptedToken;
}
public void setEncryptedToken(String encryptedToken) {
this.encryptedToken = encryptedToken;
}
} }
But when I try to parse this xml encryptedToken
is always null
. 但是,当我尝试解析此xml时,
encryptedToken
始终为null
。 What is wrong with my code? 我的代码有什么问题?
UPDATE: 更新:
Here is my sample code from main
: 这是我来自
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());
}
Actually it's not real code, because I receive xml from web service, but everything else works good, question is only in xml parsing. 实际上,这不是真正的代码,因为我从Web服务接收到xml,但其他所有工作都很好,问题仅在xml解析中。
UPDATE 2: 更新2:
I've found that if I change my xml like this: 我发现如果我像这样更改xml:
String xml = "<root> \n <EncryptedToken>11d8575638eaa52b</EncryptedToken> \n </root>";
And write class like this: 并编写这样的类:
@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;
}
}
Everyrthing is ok. 一切都很好。
So my question is: how is it possible to parse xml document without root relement using SimpleXml 所以我的问题是:使用SimpleXml如何在没有root补充的情况下解析xml文档
Note: I need to use SimpleXml, I can't use any other parser. 注意:我需要使用SimpleXml,不能使用任何其他解析器。
尝试使用@Text,而不是@Element,然后可以使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.