如何使用Java读取Xml属性? [英] How can I read Xml attributes using Java?

查看:108
本文介绍了如何使用Java读取Xml属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个如下所示的xml文档:

 <?xml version = ? 1.0 > 
< xml_api_reply version =1>
< weather section =0row =0mobile_zipped =1mobile_row =0tab_id =0module_id =0>
< forecast_information>
< city data =Cordova,Andalusia/>
< postal_code data =cordoba/>
< latitude_e6 data =/>
< longitude_e6 data =/>
< forecast_date data =2012-07-18/>
< current_date_time data =1970-01-01 00:00:00 +0000/>
< unit_system data =SI/>
< / forecast_information>

我想显示城市数据 postal_code System.out.println()的帮助下,>和日期属性。



有什么想法?

解决方案

我有解决方案。我从未在此博客或其他任何内容中看到此解决方案。我希望它对其他人有用。

  package Main; 

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;

公共类XmlTest
{

public static void main(String argv [])
{

try
{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File(xmlPrueba.xml));
doc.getDocumentElement()。normalize();



System.out.println(City:+
documento.getDocumentElement()。getChildNodes()。item(0).getFirstChild()。 。getChildNodes()项(0).getAttributes()getNamedItem( 数据)getNodeValue())。。

System.out.println(邮政编码:+
documento.getDocumentElement()。getChildNodes()。item(0).getFirstChild()。getChildNodes()。item(1 ).getAttributes()getNamedItem( 数据)getNodeValue())。。

System.out.println(Date:+
documento.getDocumentElement()。getChildNodes()。item(0).getFirstChild()。getChildNodes()。item(4) 。.getAttributes()getNamedItem( 数据)getNodeValue())。

} catch(例外e)
{
e.printStackTrace();
}

}
}

或更多轻松
......

  System.out.println(城市:+ 
。doc.getDocumentElement()的getElementsByTagName( forecast_information)项目(0).getChildNodes()项(0).getAttributes()getNamedItem( 数据)getNodeValue())。。

System.out.println(邮政编码:+
doc.getDocumentElement()。getElementsByTagName(forecast_information)。item(0).getChildNodes()。item(1) 。.getAttributes()getNamedItem( 数据)getNodeValue())。

System.out.println(Date:+
doc.getDocumentElement()。getElementsByTagName(forecast_information)。item(0).getChildNodes()。item(4)。 。的getAttributes()getNamedItem( 数据)getNodeValue())。

.....

感谢您的帮助!!! / p>

Given an xml document that looks like the following:

 <?xml version="1.0"?>
     <xml_api_reply version="1">
      <weather section="0" row="0" mobile_zipped="1" mobile_row="0"     tab_id="0" module_id="0">
        <forecast_information>
            <city data="Cordova, Andalusia"/>
            <postal_code data="cordoba"/>
            <latitude_e6 data=""/>
            <longitude_e6 data=""/>
            <forecast_date data="2012-07-18"/>
            <current_date_time data="1970-01-01 00:00:00 +0000"/>
            <unit_system data="SI"/>
         </forecast_information>

I want to show city data, postal_code and date attributes with help of System.out.println().

Any idea?

解决方案

I have the solution. This solution I never saw in this blog or any other. I hope it is useful for others.

package Main;

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;

public class XmlTest
{

    public static void main(String argv[]) 
    {

        try 
        {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(new File("xmlPrueba.xml"));
            doc.getDocumentElement().normalize();



            System.out.println("City: " +
                    documento.getDocumentElement().getChildNodes().item(0).getFirstChild().getChildNodes().item(0).getAttributes().getNamedItem("data").getNodeValue());

            System.out.println("Postal Code: " +
                    documento.getDocumentElement().getChildNodes().item(0).getFirstChild().getChildNodes().item(1).getAttributes().getNamedItem("data").getNodeValue());

            System.out.println("Date: " +
                    documento.getDocumentElement().getChildNodes().item(0).getFirstChild().getChildNodes().item(4).getAttributes().getNamedItem("data").getNodeValue());

        } catch (Exception e) 
        {
            e.printStackTrace();
        }

    }
}

or more easy ......

            System.out.println("City: " +
                doc.getDocumentElement().getElementsByTagName("forecast_information").item(0).getChildNodes().item(0).getAttributes().getNamedItem("data").getNodeValue());

            System.out.println("Postal Code: " +
                doc.getDocumentElement().getElementsByTagName("forecast_information").item(0).getChildNodes().item(1).getAttributes().getNamedItem("data").getNodeValue());

            System.out.println("Date: " +
                doc.getDocumentElement().getElementsByTagName("forecast_information").item(0).getChildNodes().item(4).getAttributes().getNamedItem("data").getNodeValue());

.....

Thanks for the help!!!

这篇关于如何使用Java读取Xml属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆