use of org.dom4j.Node in project application by collectionspace.
the class XTmplDocument method setTexts.
@SuppressWarnings("unchecked")
public void setTexts(String key, String[] texts) {
Node basis = document.selectSingleNode(attach.get(key));
Element parent = basis.getParent();
int pos = parent.indexOf(basis);
List<Node> nodes = (List<Node>) parent.content();
List<Node> after = new ArrayList(nodes.subList(pos + 1, nodes.size()));
basis.detach();
if (texts.length > 0) {
for (Node n : after) n.detach();
for (String text : texts) {
Node renewed = (Node) basis.clone();
renewed.setText(text);
parent.add(renewed);
}
for (Node n : after) parent.add(n);
}
}
use of org.dom4j.Node in project application by collectionspace.
the class TestXTmpl method testBasic.
@SuppressWarnings("unchecked")
@Test
public void testBasic() throws Exception {
Document doc = getDocument("tmpl1.xml");
XTmplTmpl template = XTmplTmpl.compile(doc);
XTmplDocument document = template.makeDocument();
document.setText("c", "hello-c");
document.setContents("d", getDocument("tmpl2.xml").getRootElement());
document.setTexts("x", new String[] { "y", "z" });
assertEquals("hi", document.getDocument().selectSingleNode("/a/b/d/e/f").getText());
assertEquals("hello-c", document.getDocument().selectSingleNode("/a/b/c").getText());
List<Node> x = document.getDocument().selectNodes("/a/b/w/x");
assertEquals(2, x.size());
assertEquals("y", x.get(0).getText());
assertEquals("z", x.get(1).getText());
}
use of org.dom4j.Node in project tutorials by eugenp.
the class Dom4JParser method getNodeById.
public Node getNodeById(String id) {
try {
SAXReader reader = new SAXReader();
Document document = reader.read(file);
List<Node> elements = document.selectNodes("//*[@tutId='" + id + "']");
return elements.get(0);
} catch (DocumentException e) {
e.printStackTrace();
return null;
}
}
use of org.dom4j.Node in project tutorials by eugenp.
the class Dom4JParserUnitTest method getElementsListByTitleTest.
@Test
public void getElementsListByTitleTest() {
parser = new Dom4JParser(new File(fileName));
Node element = parser.getElementsListByTitle("XML");
assertEquals("java", element.valueOf("@type"));
assertEquals("02", element.valueOf("@tutId"));
assertEquals("XML", element.selectSingleNode("title").getText());
assertEquals("title", element.selectSingleNode("title").getName());
}
use of org.dom4j.Node in project tutorials by eugenp.
the class Dom4JParserUnitTest method generateNewDocumentTest.
@Test
public void generateNewDocumentTest() {
parser = new Dom4JParser(new File(fileName));
parser.generateNewDocument();
File newFile = new File("src/test/resources/example_dom4j_new.xml");
assertTrue(newFile.exists());
parser.setFile(newFile);
Node element = parser.getNodeById("01");
assertEquals("XML with Dom4J", element.selectSingleNode("title").getText());
}
Aggregations