Contribution:
xpathAPI
Headline
Exercise XML processing with Language:XPath embedded in Language:Java
Characteristics
Companies are represented in Language:XML and processed by XPath and Java. XPath is embedded into Java code which makes it possible to process query results in Java. Query results are bound to DOM-like node sets, and one can iterate over those and mutate them, thereby implementing impure transformations.
Illustration
The data model is implemented as xml files (sampleCompany.xml).
Feature:Open serialization is implemented using Technology:DOM Document objects:
/**
* Load a document from a file
*/
public static Document loadDocument(String filename) {
try {
// Create a builder factory
DocumentBuilderFactory factory =
DocumentBuilderFactory.
newInstance();
// Create the builder
DocumentBuilder builder =
factory.
newDocumentBuilder();
// Deserialization by parsing
Document doc =
builder.
parse(new File(filename));
// Done
return doc;
} catch (SAXException e) {
} catch (ParserConfigurationException e) {
} catch (IOException e) {
}
// Return null for any sort of problem
return null;
}
/**
* Save a document to a file.
*/
public static boolean saveDocument(Document doc, String filename) {
try {
// Prepare the DOM document for writing
Source source = new DOMSource(doc);
// Prepare the output file
File file = new File(filename);
Result result = new StreamResult(file);
// Creater a transformer factory
TransformerFactory xfactory =
TransformerFactory.
newInstance();
// Create a transformer
Transformer transformer =
xfactory.
newTransformer();
// Force pretty printing
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
// Serialization by transformation
transformer.transform(source, result);
// Done
return true;
} catch (TransformerConfigurationException e) {
} catch (TransformerException e) {
}
// Return false for any sort of problem
return false;
}
Feature:Total and Feature:Cut are implemented by using Technology:DOM trees and Language:XPath queries:
public static double total(Document doc)
throws Exception {
// The aggregation variable
double total = 0;
// Get the matching elements
NodeList nodelist = XPathAPI.selectNodeList(doc, "//salary");
// Process the elements in the nodelist
for (int i=0; i<nodelist.getLength(); i++) {
// Get element
Element elem = (Element)nodelist.item(i);
total += Double.parseDouble(elem.getTextContent());
}
return total;
}
The Language:XPath processor is implemented by Technology:Xalan-Java.
Test cases are implemented for all Namespace:Features.
Relationships
For DOM-like implementations of in-memory XML processing see Contribution:dom, Contribution:jdom and Contribution:xom.
For a query-based implementation of in-memory XML processing see Contribution:xpathAPI.
For push-based XML processing see Contribution:sax.
For Object/XML mapping see Contribution:jaxbChoice (XSD with choice for different subunits), Contribution:jaxbComposition (XSD with object composition), Contribution:jaxbExtension (XSD with type extension) and Contribution:jaxbSubstitution (XSD with substitution groups).
Architecture
The contribution follows a standardized structure:
- inputs contains input files for tests
- src/main/java contains the following packages:
- org.softlang.company.features for implementations of Functional requirements.
- src/test/java contains the following packages:
- org.softlang.company.tests for Technology:JUnit test cases for Namespace:Features.
Usage
This contribution uses Technology:Gradle for building. Technology:Eclipse is supported.
See https://github.com/101companies/101simplejava/blob/master/README.md
There are no revisions for this page.
User contributions
User edits
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.