java - 如何解析 XML 标签 <m :properties> for my Android application using Java?

标签 java android xml

我需要显示报价日期、费率类型和费率。 我正在使用节点,但我不确定它是一个元素还是一个属性,它可能是一个命名空间,不知道如何引用它。

这是 XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://data.treasury.gov/Feed.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">DailyTreasuryLongTermRateData</title>
  <id>http://data.treasury.gov/feed.svc/DailyTreasuryLongTermRateData</id>
  <updated>2016-04-16T00:05:04Z</updated>
  <link rel="self" title="DailyTreasuryLongTermRateData" href="DailyTreasuryLongTermRateData" />
  <entry>
    <id>http://data.treasury.gov/Feed.svc/DailyTreasuryLongTermRateData(11440)</id>
    <title type="text"></title>
    <updated>2016-04-16T00:05:04Z</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="DailyTreasuryLongTermRateDatum" href="DailyTreasuryLongTermRateData(11440)" />
    <category term="TreasuryDataWarehouseModel.DailyTreasuryLongTermRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:Id m:type="Edm.Int32">11440</d:Id>
        <d:QUOTE_DATE m:type="Edm.DateTime">2015-04-01T00:00:00</d:QUOTE_DATE>
        <d:EXTRAPOLATION_FACTOR>N/A</d:EXTRAPOLATION_FACTOR>
        <d:RATE_TYPE>BC_20year</d:RATE_TYPE>
        <d:RATE m:type="Edm.Double">2.23</d:RATE>
      </m:properties>
    </content>`enter code here`

这是我到目前为止的代码:

    @Override
    protected void onPostExecute(String result) {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = null;
        org.w3c.dom.Document doc = null;
        try {
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(result));
            doc = documentBuilder.parse(is);
            doc.normalize();


            Node rootNode = doc.getDocumentElement();
            String rootNodeName = rootNode.getNodeName();
            tvNodeName.setText(rootNodeName);

            NodeList nlProperties = doc.getElementsByTagName("d:");
            Node nProperties = nlProperties.item(0);
            Node nPropertiesText = nProperties.getFirstChild();
            tvQuoteDate.setText(nPropertiesText.getNodeValue());

            NodeList nlPoint = doc.getElementsByTagName("d:");
            Node nPoint = nlPoint.item(0);
            NamedNodeMap nnm = nPoint.getAttributes();

            Node rateType, rate;
            rateType = nnm.getNamedItem("RATE_TYPE");
            rate = nnm.getNamedItem("RATE");

            tvRateType.setText(rateType.getNodeValue());
            tvRate.setText(rate.getNodeValue());


        } catch (Exception e) {

        }
    }

最佳答案

我建议您使用 Simple - XML 3rd party 库。

http://simple.sourceforge.net/home.php.

检查他们的第二页,其中有教程和代码 fragment 。

您可以使用“m”作为前缀,它会在属性之前添加“m”。

这取自他们的教程页面,请务必在那里阅读!:

@Namespace(reference="your refrence", prefix="m")
<小时/>

为了让您的生活更轻松,您还可以尝试converting_xml_to_pojo网站,它会自动为您创建类,您只需要复制页面并添加元素/属性附加信息,例如@Element,@Attribute等,如教程中提到的

关于java - 如何解析 XML 标签 <m :properties> for my Android application using Java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36671226/

相关文章:

Java 应用程序开发(桌面和 Web)

android - 我如何在android中将对象从一个应用程序发送到另一个应用程序

xml - DataSet.DataTable.DataRow(单个)到 XML 字符串

java - boolean 数组问题

java - Java 中使用访问器方法的条件语句

android - google play services 8.3.0 - 获取 g+ 封面图片(已弃用 Plus.PeopleApi.getCurrentPerson() )

java - 如果父节点和子节点同名,如何找到一个节点?

android - 使 Android 布局可滚动

java - Jackson 序列化 - 过滤后删除空属性

android - 启用使用主机 GPU 时 Espresso 测试卡住