Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read Xml attributes using Java?

Tags:

java

xml

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?

like image 598
Ignox Avatar asked May 12 '26 00:05

Ignox


2 Answers

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

like image 84
Ignox Avatar answered May 14 '26 12:05

Ignox


Just a little cleaned up with type declarations, basic null checks:

Start with any node in the value of 'dom'

NodeList l = dom.getElementsByTagName("tagname");

for (int j=0; j<l.getLength(); ++j) {
    Node prop = l.item(j);
    NamedNodeMap attr = prop.getAttributes();
    if (null != attr) {
        Node p = attr.getNamedItem("data");
        log.debug(p.getNodeValue());
    }
}
like image 29
Rondo Avatar answered May 14 '26 12:05

Rondo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!