Skip to content
Snippets Groups Projects
Commit 26c0c184 authored by Goik Martin's avatar Goik Martin
Browse files

Exercise Jdom table manipulations

parent 75941930
No related branches found
No related tags found
No related merge requests found
package dom.table;
import java.io.IOException;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
/**
* How to use XPath expressions to search for <img>
* elements in XHTML document instances.
*
* @author goik
*/
public class ReorderTableData {
private final SAXBuilder builder = new SAXBuilder();
/**
*
*/
public ReorderTableData() {
builder.setErrorHandler(new MySaxErrorHandler(System.err));
}
/**
* Read an XHTML document and search for images. The image filenames will be
* written to standard out.
* @param xhtmlFilename The XHTML's filename to be examined.
* @throws JDOMException
* @throws IOException
*/
public void process(final String xhtmlFilename) throws JDOMException, IOException {
final Document htmlInput = builder.build(xhtmlFilename);
final XPathExpression<Object> xpath = XPathFactory.instance().compile("//img");
final List<Object> images = xpath.evaluate(htmlInput);
for (Object o: images) {
final Element image = (Element ) o;
System.out.print(image.getAttributeValue("src") + " ");
}
}
}
\ No newline at end of file
package dom.table1;
/**
* Driver to test XPath based searching of <img> elements
* within a given HTML document.
*
*/
public class Driver {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
final TablePresentation ao = new TablePresentation();
// The following HTML document is being searched
// for <img> elements.
ao.process("src/dom/table/persons.xml");
}
}
\ No newline at end of file
package dom.table1;
import java.io.IOException;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
import dom.table.MySaxErrorHandler;
public class TablePresentation {
private final SAXBuilder builder = new SAXBuilder();
public TablePresentation() {
builder.setErrorHandler(new MySaxErrorHandler(System.err));
}
public void process(final String xmlFilename) throws JDOMException,
IOException {
// Create HTML <table><tr><th>Name</th><th>Birthday</th></tr></table>
final Element table = new Element("table");
final Element tableHeader = new Element("tr");
table.addContent(tableHeader);
tableHeader.addContent(new Element("th").addContent("Name"));
tableHeader.addContent(new Element("th").addContent("Birthday"));
// Read addressbook from XML input file
final Document addressbook = builder.build(xmlFilename);
// Search for <person> elements
@SuppressWarnings("unchecked")
final XPathExpression<Element> xpath = (XPathExpression<Element>) (Object) XPathFactory
.instance().compile("/addressbook/person");
final List<Element> persons = xpath.evaluate(addressbook);
// Create a <tr> for each <person> element
boolean setBackground = true;
for (Element person : persons) {
final Element tr = new Element("tr");
if (setBackground) {
tr.setAttribute("style", "background-color: #FFFF88;");
setBackground = false;
} else {
setBackground = true;
}
table.addContent(tr);
tr.addContent(new Element("td").addContent(person.getChild("name").getText()));
tr.addContent(new Element("td").addContent(person.getChild("birthday").getText()));
}
final Format outFormat = Format.getPrettyFormat();
// Serialize to standard output
final XMLOutputter printer = new XMLOutputter(outFormat);
printer.output(table, System.out);
}
}
\ No newline at end of file
package dom.table2;
/**
* Driver to test XPath based searching of <img> elements
* within a given HTML document.
*
*/
public class Driver {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
final TablePresentation ao = new TablePresentation();
// The following HTML document is being searched
// for <img> elements.
ao.process("src/dom/table/persons.xml");
}
}
\ No newline at end of file
package dom.table2;
import java.io.IOException;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
import dom.table.MySaxErrorHandler;
public class TablePresentation {
private final SAXBuilder builder = new SAXBuilder();
public TablePresentation() {
builder.setErrorHandler(new MySaxErrorHandler(System.err));
}
public void process(final String xmlFilename) throws JDOMException,
IOException {
// Read addressbook from XML input file
final Document addressbook = builder.build(xmlFilename);
// Create HTML <table/>
final Element table = new Element("table");
// Search for <person> elements
@SuppressWarnings("unchecked")
final XPathExpression<Element> xpath = (XPathExpression<Element>) (Object) XPathFactory
.instance().compile("/addressbook/person");
final List<Element> persons = xpath.evaluate(addressbook);
// Create the name row
final Element nameRow = new Element("tr");
table.addContent(nameRow);
nameRow.addContent(new Element("th").addContent("Name"));
// Create the name birthday row
final Element birthdayRow = new Element("tr");
table.addContent(birthdayRow);
birthdayRow.addContent(new Element("th").addContent("Birthday"));
for (Element person : persons) {
nameRow.addContent(new Element("td").addContent(person.getChild("name").getText()));
birthdayRow.addContent(new Element("td").addContent(person.getChild("birthday").getText()));
}
final Format outFormat = Format.getPrettyFormat();
final XMLOutputter printer = new XMLOutputter(outFormat);
printer.output(table, System.out);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment