如何使用Java解析Android应用程序的XML标记<m:properties>?

时间:2016-04-17 00:03:56

标签: 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) {

        }
    }

1 个答案:

答案 0 :(得分:0)

我建议您使用Simple - XML第三方库。

public int[][] mirror(int[][] original) {
     int[][] mirror = original;
     for (int i=0; i<original.length; i++) {
         original[i] = reverseArray(original[i]);
     }
     return mirror;
}


public int[] reverseArray(int[] array) {
    for (i = 0; i < array.length / 2; i++) {
        int temp = array[i];
        array[i] = array[array.length - 1 - i];
        array[array.length - 1 - i] = temp;
    }
    return array;
}

检查他们的第二页是否有教程和代码片段。

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

这是从他们的教程页面中获取的,请确保您在那里阅读!:

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

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