【发布时间】:2014-08-18 06:28:58
【问题描述】:
我有一个 xml 文件
<Response>
<StatusCode>0</StatusCode>
<StatusDetail>OK</StatusDetail>
<AccountInfo>
<element1>value</element1>
<element2>value</element2>
<element3>value</element2>
<elementN>value</elementN>
</AccountInfo>
</Response>
我想在 AccountInfo 中解析我的元素,但我不知道元素标签名称。
现在我正在使用并拥有此代码进行测试,但将来我会在 AccountInfo 中收到更多元素,但我不知道有多少或有名称
String name="";
String balance="";
Node accountInfo = document.getElementsByTagName("AccountInfo").item(0);
if (accountInfo.getNodeType() == Node.ELEMENT_NODE){
Element accountInfoElement = (Element) accountInfo;
name = accountInfoElement.getElementsByTagName("Name").item(0).getTextContent();
balance = accountInfoElement.getElementsByTagName("Balance").item(0).getTextContent();
}
【问题讨论】:
-
如果您不知道可能有哪些标签(以及它们的含义),您想如何以有意义的方式提取信息?
-
您确定要动态显示吗?如果您有 n 个以动态方式解析的不同变量,则可能很难使用它们(例如,如果您使用某种算法来计算某个值)。这些值将(或多或少)在列表中表示。通常的方法是为一组特定的输入提供一个实现。还是您只想将值转储到数据库中?
-
This answer 可以帮助您入门。
-
我想将此信息发送到其他模块,该模块可以在前端打印它。我的程序将与不同的提供商合作,他们有不同的信息...提供商列表非常大...超过 300