我有一个类似这样的 xml。
<card>
<name>VISA</name>
<id>0</id>
<length>
<value>16</value>
</length>
<bin>
<range>
<start>4</start>
</range>
</bin>
</card>
这是我用来加载并解析此 xml 的代码。
private XMLConfiguration loadInputXml(final String responseXml)
throws ConfigurationException {
final XMLConfiguration xmlConfig = new XMLConfiguration();
xmlConfig.setDelimiterParsingDisabled(true);
xmlConfig.setValidating(false);
xmlConfig.load(new ByteArrayInputStream(responseXml.getBytes()));
return xmlConfig;
}
现在我尝试通过此代码获取值
XMLConfiguration xmlConfig = loadInputXml(xmlString);
List<HierarchicalConfiguration> cardList = xmlConfig
.configurationsAt("card");
我无法获取子节点,也没有收到任何错误,因此我无法找到其根本原因。 需要帮忙。 提前致谢!!!!
最佳答案
xmlConfig.configurationAt
未获取任何值,因为 card
是 XML
的根元素,因此调用configurationsAt("card")
会使其在根元素中搜索元素 card
,但这里没有这样的元素,所以你什么也得不到。
对我来说,你尝试做的事情没有多大意义,但如果出于未知原因,你确实需要使用 configurationsAt
访问根元素,请使用空的 String
作为参数,使其获取根元素作为下一个:
List<HierarchicalConfiguration> cardList = xmlConfig.configurationsAt("");
<小时/>
您应该做的只是使用 XMLConfiguration
类的实例,根据预期类型通过 getXXX
方法之一访问您的属性。
例如,假设我想获取name
和length
的值,我将继续如下操作:
XMLConfiguration xmlConfig = ...
// Get as String the text content of the element name
String name = xmlConfig.getString("name");
// Get as int the text content of the element value of the first element length
int length = xmlConfig.getInt("length(0).value");
关于java - xmlConfig.configurationAt 未获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40610483/