如何使用Java获取xml节点的属性值

如何使用Java获取xml节点的属性值,第1张

如何使用Java获取xml节点的属性值

由于您的问题较为笼统,请尝试使用Java中提供的XML解析器来实现。如果您特定于解析器,请在此处更新您尝试过的代码

<?xml version="1.0" encoding="UTF-8"?><ep>    <source type="xml">TEST</source>    <source type="text"></source></ep>documentBuilderFactory domFactory = documentBuilderFactory.newInstance();documentBuilderFactory factory = documentBuilderFactory.newInstance();documentBuilder builder = factory.newdocumentBuilder();document doc = builder.parse("uri to xmlfile");XPathFactory xPathfactory = XPathFactory.newInstance();XPath xpath = xPathfactory.newXPath();XPathexpression expr = xpath.compile("//ep/source[@type]");NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);for (int i = 0; i < nl.getLength(); i++){    Node currentItem = nl.item(i);    String key = currentItem.getAttributes().getNamedItem("type").getNodevalue();    System.out.println(key);}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5615563.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存