Search in sources :

Example 66 with Node

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);
    }
}
Also used : Node(org.dom4j.Node) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 67 with Node

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());
}
Also used : Node(org.dom4j.Node) Document(org.dom4j.Document) Test(org.junit.Test)

Example 68 with Node

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;
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Node(org.dom4j.Node) DocumentException(org.dom4j.DocumentException) Document(org.dom4j.Document)

Example 69 with Node

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());
}
Also used : Node(org.dom4j.Node) File(java.io.File) Test(org.junit.Test)

Example 70 with Node

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());
}
Also used : Node(org.dom4j.Node) File(java.io.File) Test(org.junit.Test)

Aggregations

Node (org.dom4j.Node)253 Document (org.dom4j.Document)83 Element (org.dom4j.Element)61 ArrayList (java.util.ArrayList)56 List (java.util.List)54 Test (org.junit.Test)40 SAXReader (org.dom4j.io.SAXReader)28 File (java.io.File)25 Iterator (java.util.Iterator)18 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)18 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)18 HashMap (java.util.HashMap)16 URL (java.net.URL)15 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)15 InputStream (java.io.InputStream)14 Attribute (org.dom4j.Attribute)14 Image (java.awt.Image)12 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)11 VFSItem (org.olat.core.util.vfs.VFSItem)10 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)10