2

次のような xml ドキュメントがあるとします。

 <?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>

の助けを借りて、都市データpostal_code、および日付属性を表示したいと考えていますSystem.out.println()

何か案が?

4

6 に答える 6

9

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!!!

于 2012-07-20T21:02:41.280 に答える
2

Java には、 SAXStAXおよびDOM APIなど、XML 解析用のいくつかの機能が搭載されています。DOM API は、初心者にとって最も快適なものです。この素敵なチュートリアルをご覧ください。

ここで XSLT について Thom と矛盾しなければなりません。強力な API である一方で、かなり複雑で、初心者にとっては威圧的でイライラするかもしれません。

于 2012-07-19T11:49:19.507 に答える
1

DOM と SAX は、どちらも低レベルの API であり、それらを扱うのは困難です。この質問は

Java での XML 解析

したがって、これは SAX と DOM で実現できますが、Apache Digesterを使用するとより簡単になります。これは、データ バインディングとも呼ばれます。

これがあなたを助けることを願っています。

于 2012-07-19T12:16:34.867 に答える
1

これを見てください:

JDOM : http://www.javaworld.com/jw-05-2000/jw-0518-jdom.html

SAX : http://en.wikipedia.org/wiki/Simple_API_for_XML

Java XML の単純なパーサー

于 2012-07-19T11:49:37.090 に答える
0

XSLT を参照してください。使ったことはありませんが、XML 文書の書式設定を行います。

于 2012-07-19T11:48:16.200 に答える