How can I read Xml attributes using Java?

后端 未结 6 1433
一生所求
一生所求 2020-12-10 21:44

Given an xml document that looks like the following:

 
     

        
相关标签:
6条回答
  • 2020-12-10 22:24

    Look up XSLT. I've never used it, but it does formatting of XML documents.

    0 讨论(0)
  • 2020-12-10 22:28

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

    0 讨论(0)
  • 2020-12-10 22:31

    DOM and SAX, both are low level APIs , It's hard to work with them. This question is about

    XML Parsing in java

    So this can be achieved by SAX and DOM but using Apache Digester will be more easy. This is also known as Data Binding.

    Hope this will help you out.

    0 讨论(0)
  • 2020-12-10 22:34

    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());
        }
    }
    
    0 讨论(0)
  • 2020-12-10 22:37

    Java has several features for XML parsing on board - the SAX, StAX and DOM APIs. The DOM API is the most comfortable one for beginners. Take a look at this nice tutorial.

    I must contradict Thom here on XSLT. While being a powerful API, it is rather complex and may be intimidating and frustrating for beginners.

    0 讨论(0)
  • 2020-12-10 22:47

    You should look at this :

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

    and

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

    Simple parser for Java XML

    0 讨论(0)
提交回复
热议问题