Class JaxpExamples
java.lang.Object
net.jrodolfo.java_evolution.java04.xml.JaxpExamples
Demonstrates JAXP XML processing, standardized in J2SE 1.4.
The parser path intentionally uses only the Java 4-era JAXP and DOM APIs. Modern XML security properties were added later and are discussed in the package documentation instead of being mixed into this historical example. The example accepts trusted in-memory XML only.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionattributeValue(String xml, String elementName, String attributeName) Reads an attribute from the first element with the requested name.parseDocument(String xml) Parses trusted XML text into a DOM document.rootElementName(String xml) Reads the root element name from XML.Reads the text from the first element with the requested name.transformToText(Document document) Transforms a DOM document back to XML text.
-
Constructor Details
-
JaxpExamples
public JaxpExamples()
-
-
Method Details
-
parseDocument
public Document parseDocument(String xml) throws ParserConfigurationException, IOException, SAXException Parses trusted XML text into a DOM document.- Parameters:
xml- trusted XML input- Returns:
- parsed document
- Throws:
ParserConfigurationException- when the parser cannot be createdIOException- when the input cannot be readSAXException- when the XML is invalid
-
rootElementName
public String rootElementName(String xml) throws ParserConfigurationException, IOException, SAXException Reads the root element name from XML.- Parameters:
xml- trusted XML input- Returns:
- root element name
- Throws:
ParserConfigurationException- when the parser cannot be createdIOException- when the input cannot be readSAXException- when the XML is invalid
-
attributeValue
public String attributeValue(String xml, String elementName, String attributeName) throws ParserConfigurationException, IOException, SAXException Reads an attribute from the first element with the requested name.- Parameters:
xml- trusted XML inputelementName- element nameattributeName- attribute name- Returns:
- attribute value
- Throws:
ParserConfigurationException- when the parser cannot be createdIOException- when the input cannot be readSAXException- when the XML is invalid
-
textValue
public String textValue(String xml, String elementName) throws ParserConfigurationException, IOException, SAXException Reads the text from the first element with the requested name.- Parameters:
xml- trusted XML inputelementName- element name- Returns:
- element text
- Throws:
ParserConfigurationException- when the parser cannot be createdIOException- when the input cannot be readSAXException- when the XML is invalid
-
transformToText
Transforms a DOM document back to XML text.- Parameters:
document- parsed document- Returns:
- XML text
- Throws:
TransformerException- when the document cannot be transformed
-