我需要从 url url xml 解析 xml
<?xml version="1.0" encoding="UTF-8"?>
<exchangerates>
<row>
<exchangerate ccy="RUR" base_ccy="UAH" buy="0.33291" sale="0.33291"/>
</row>
<row>
<exchangerate ccy="EUR" base_ccy="UAH" buy="18.60253" sale="18.60253"/>
</row>
<row>
<exchangerate ccy="USD" base_ccy="UAH" buy="14.97306" sale="14.97306"/>
</row>
</exchangerates>
我想要获取使用“14.97306”转换我的货币的属性
(like mycurrency = 10 )
(like usd = 14.97306 )
(mycurrency * usd = 149.7306)
最佳答案
这是 Java 的基本设置。您需要阅读 XPATH 查询,并且需要添加异常处理等。但我相信这会让您入门。
// open the URL
URL url = ....
InputStream is = url.openStream();
// build a document parser
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
// parse the document from the URL
Document d = builder.parse(is);
// set up XPATH to examine/query the XML
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
// use XPATH query to find the required information
String sale = xpath.evaluate("/exchangerates/row[@ccy='USD']/@sale", d);
System.out.println("USD sale is "+sale);
关于javascript - javascript如何从url解析xml获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27166456/