How to read an XML file using Java?

前端 未结 6 1387
醉梦人生
醉梦人生 2020-12-21 15:55

I need to read an XML file using Java. Its contents are something like


    
        C:/Input.csv

        
相关标签:
6条回答
  • 2020-12-21 16:14

    Check out Java's JAXP APIs which come as standard. You can read the XML in from the file into a DOM (object model), or as SAX - a series of events (your code will receive an event for each start-of-element, end-of-element etc.). For both DOM and SAX, I would look at an API tutorial to get started.

    Alternatively, you may find JDOM easier/more intuitive to use.

    0 讨论(0)
  • 2020-12-21 16:18

    xstream would do very nicely here. Check out the one page tutorial

    0 讨论(0)
  • 2020-12-21 16:20

    JAXB is another technology that might suit your needs.

    0 讨论(0)
  • 2020-12-21 16:26

    Another suggestion: Try out Commons digester. This allows you to develop parsing code very quickly using a rule-based approach. There's a tutorial here and the library is available here

    I also agree with Brian and Alzoid in that JAXB is great to get you up and running quickly. You can use the xjc binding compiler that ships with the JDK to auto generate your Java classes given an XML schema.

    0 讨论(0)
  • 2020-12-21 16:26

    There are two major ways to parse XML with Java. The first is to use a SAX parser see here

    which is fairly simple.

    The second option is to use a DOM parser see here

    which is more complicated but gives you more control.

    0 讨论(0)
  • 2020-12-21 16:35

    You can user external libraries like Castor https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-1046622.html I have used castor in past. Here are few other links that might help. http://www.xml-training-guide.com/e-xml27.html

    http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/XMLReader.html http://www.cafeconleche.org/books/xmljava/chapters/ch07.html

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