XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
XML is a software- and hardware-independent tool for storing and transporting data.
How to work with XML in Java?
Package java.xml
Removed: JAXB (JSR 31, 222) and JAX-WS (JSR 224)
Data bind
Tree Model
Streaming
XPath
JAXP (Java API for XML Processing) - это набор API (SAX + DOM + валидация DTD + XSLT)
SAX (Push Model) → StAX (Pull Model) - последовательное чтение из источника XML
DOM (tree) → JAXB (mapping) - API для полного зачитывания XML и получения в приложении его готового представления в объектах Java
XSL, XSLT
XPath - язык запросов к XML
StAX is a pull API. SAX is a push API.
StAX can do both XML reading and writing. SAX can only do XML reading.
StAX can use iterator and cursor readers
XMLInputFactory
javax.xml.stream.XMLInputFactory
- root component
This class can create both an XMLStreamReader
and an XMLEventReader
Can set various properties on the XMLInputFactory
instance using the setProperty()
method
XMLEventReader
hasNext(): boolean
nextEvent(): XMLEvent
XMLEvent
getEventType(): int
asStartElement(): StartElement
asEndElement(): EndElement
asCharacters(): Characters
isEndElement(): boolean
asStartElement(): boolean
XMLStreamConstants
ATTRIBUTE
CDATA
CHARACTERS
COMMENT
DTD
END_DOCUMENT
END_ELEMENT
ENTITY_DECLARATION
XMLStreamConstants
ENTITY_REFERENCE
NAMESPACE
NOTATION_DECLARATION
PROCESSING_INSTRUCTION
SPACE
START_DOCUMENT
START_ELEMENT
XMLOutputFactory
javax.xml.stream.XMLOutputFactory
- root component
This class can create both an XMLStreamWriter
and an XMLEventWriter
Can set various properties on the XMLOutputFactory
instance using the setProperty()
method
XMLEventFactory.newInstance(): XMLEventFactory
createStartDocument(): StartDocument
createStartElement(String prefix, String namespaceUri, String localName): StartElement
createNamespace(String prefix, String namespaceUri): Namespace
createAttribute(String localName, String value): Attribute
createEndElement(String prefix, String namespaceUri, String localName): EndElement
createEndDocument(): EndDocument
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
XPathExpression xPathExpression = xpath.compile("THIS_IS_YOUR_EXPRESSIONS");
NodeList nodes = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET);
XPathConstants
STRING
NUMBER
BOOLEAN
NODE
NODESET
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<developers>
<developer id="1">
<name>Andrew</name>
<age>25</age>
<position>Middle</position>
<language>Java</language>
</developer>
<developer id="2">
<name>Dima</name>
<age>21</age>
<position>Junior</position>
<language>JS</language>
</developer>
</developers>
XPathExpression xPathExpression = xpath.compile(
"/developers/developer[position='" + position + "']/name/text()"
);
XPathExpression xPathExpression = xpath.compile(
"/developers/developer[age<" + age + "]/name/text()"
);
XPathExpression xPathExpression = xpath.compile(
"/developers/developer[@id='" + id + "']/name/text()"
);