I want to see the easiest and most compact XML parsing algorithm in Java.
Any libraries, classes. m?
Answer 1, Authority 100%
There is no easier, nor better worse than))) There is only a harsh reality, but if you believe the Buddha, it is not. If you need to pull out in several ways to info from the same XML of the document, then most likely cheaper by resources to build a DOM tree. If all the information is selected in one pass, then the task is more effective to solve with the help of SAX flow processing.
For the game with exotic to regular expressions, I propose to add antenr or something similar)))
p.s. Everyone remembers the anecdote about the man in Belarus?
Answer 2, Authority 400%
Unified generally accepted means for XML parsing in Java No,
Therefore, I will list the most common parsers:
DOM (Document Object Model – Operating Document Model) – Platform-independent software interface that allows programs and scripts to manage the contents of HTML and XML documents, as well as modify their structure and design. The DOM model does not impose restrictions on the document structure. Any document of a well-known structure using DOM can be represented as a tree of nodes, each node of which contains an item, attribute, text, graphic or any other object. Nodes are associated between
SAX (Simple API for XML) is an event-oriented API.
The method of serial read / write XML files.
Typically, SAX parsers require a fixed amount of memory for their work, but do not allow you to change the contents of the document. Everything that Sax-Parser does, it tells the application of the recognized elements of XML markup or the errors encountered.
JDOM (Java Document Object Model) is a free Java-Sales DOM for XML, created Taking into account the features of the language and the Java platform. JDOM integrates with Document Object Model (DOM) and Simple API for XML (SAX), supports XPath and XSLT. JDOM use external parses to generate documents. JDOM
STAX – streaming API for XML. It is the latest XML data processing standard in Java. Like a stream method, it often turns out to be the best alternative than other methods, for example, DOM and SAX, and in terms of productivity, and for convenience in the work.
All parsers have their own features:
- on the analysis of elements,
- according to the possibilities of changes in the elements in the document
- by algorithm
transition between elements (DOM, for example, reads the document fully and
Only after that it starts to dismiss it, and SAX disassembles the document
- According to the presence of a document validation algorithm (not all parsers allow you to check the document on the correctness and validity, for example, according to the XSD scheme)
- and many many other features …
All features of these parsers affect their performance, the more Parser’s bellows, the slower it works …
Sometimes you need to disassemble the document completely, sometimes you need to find all the items of a certain type, and sometimes only one item in the document.
What a paler to choose, you need to solve depending on the task set.
Answer 3, Authority 200%
What kind of parsing do you need? If DOM, then use the standard method:
documentbuilderfactory.newinstance (). NewDocumentBuilder (). Parse (File)
You can still take advantage of domj4 But in my opinion the winnings are small, and the library is not very hard to drag I want to.
If streaming, so you yourself answered your question: Use SAX
Answer 4, Authority 100%
Apache Digester , Parsing is written in 3 lines.
Always used the JDOM library, everything is much easier (:
Thank you for the answer. I want to hear any other XML parsing methods in addition to home and sax, maybe wildly exotic. I myself use
SchemaFactory SchemaFactory = SchemaFactory.Newinstance (XMLCONSTANTS.W3C_XML_SCHEMA_NS_URI); Schema Schema = SchemaFactory.newschema (New File (")); SAXPARSERFACTORY FACTORY = SAXPARSERFACTORY.NEWINSTANCE (); Factory.Setvalidating (False); Factory.Setschema (Schema);
documentbuilderfactory.newinstance (). NewDocumentBuilder (). Parse (File);
It is not new and on the Internet in bulk. Maybe there is some library or mega class, which is in a different works with XML.
topic discarded also in the topic ” XML handler in java. “. I suggest look at Stax.