List of usage examples for org.dom4j.tree DefaultDocument setRootElement
public void setRootElement(Element rootElement)
From source file:org.gbif.harvest.digir.DigirMetadataHandler.java
License:Open Source License
/** * Parse the response file and write the parsed values to their * appropriate file.//from w w w . java 2 s. c om * * @param inputStream representing harvested xml response * * @throws DocumentException thrown if parsing error occurred * @throws IOException thrown */ private void parseResponseFile(ByteArrayInputStream inputStream) throws DocumentException, IOException { // create a DOM4J tree, reading a Document from the given File SAXReader reader = new SAXReader(); reader.setEncoding("UTF-8"); Document document = reader.read(inputStream); document.setXMLEncoding("UTF-8"); // get all resource Elements List<Node> resourceEntities = (metadataRepeatingElementsXpath.get(resourceEntityRepeatingElementName)) .selectNodes(document); // iterate over resource Elements for (Node resourceEntity : resourceEntities) { // Detatch resource Element and create new Document with it DefaultDocument doc1 = new DefaultDocument(); doc1.setRootElement((Element) resourceEntity.detach()); // get all resource contact Elements List<Node> resourceContacts = (metadataRepeatingElementsXpath.get(contactEntityRepeatingElementName)) .selectNodes(doc1); // iterate over contact Elements for (Node resourceContact : resourceContacts) { // Detatch relatedEntity Element and create new Document with it DefaultDocument doc2 = new DefaultDocument(); doc2.setRootElement((Element) resourceContact.detach()); // write hasContact elements-of-interest to file fileUtils.writeValuesToFile(resourceContactsBW, metadataResourceContactElementsOfInterest.values(), doc2, namespaceMap, String.valueOf(getLineNumber())); } // write relatedEntity elements-of-interest to file fileUtils.writeValuesToFile(resourcesBW, metadataElementsOfInterest.values(), doc1, namespaceMap, String.valueOf(getLineNumber())); setLineNumber(getLineNumber() + 1); } }
From source file:org.gbif.harvest.tapir.TapirMetadataHandler.java
License:Open Source License
/** * Parse the response file and write the parsed values to their * appropriate file./* ww w. j a va 2 s . c o m*/ * * @param stream file representing harvested xml response as ByteArrayInputStream * * @throws DocumentException thrown if parsing errors occur * @throws IOException thrown */ private void parseResponseFile(ByteArrayInputStream stream) throws DocumentException, IOException { // create a DOM4J tree, reading a Document from the given File SAXReader reader = new SAXReader(); reader.setEncoding("UTF-8"); Document document = reader.read(stream); document.setXMLEncoding("UTF-8"); // get all relatedEntity Elements List<Node> relatedEntities = (metadataRepeatingElementsXpath.get(RELATEDENTITY_REPEATING_ELEMENT_NAME)) .selectNodes(document); // iterate over dataset Elements for (Node relatedEntity : relatedEntities) { // Detatch relatedEntity Element and create new Document with it DefaultDocument doc1 = new DefaultDocument(); doc1.setRootElement((Element) relatedEntity.detach()); // get all hasContact Elements List<Node> hasContacts = (metadataRepeatingElementsXpath.get(HASCONTACT_REPEATING_ELEMENT_NAME)) .selectNodes(doc1); // iterate over hasContact Elements for (Node hasContact : hasContacts) { // Detatch relatedEntity Element and create new Document with it DefaultDocument doc2 = new DefaultDocument(); doc2.setRootElement((Element) hasContact.detach()); // write hasContact elements-of-interest to file fileUtils.writeValuesToFile(hasContactBW, harvestedHasContactElementsOfInterest.values(), doc2, namespaceMap, String.valueOf(getLineNumber())); } // write relatedEntity elements-of-interest to file fileUtils.writeValuesToFile(relatedEntityBW, harvestedRelatedEntityElementsOfInterest.values(), doc1, namespaceMap, String.valueOf(getLineNumber())); setLineNumber(getLineNumber() + 1); } }
From source file:org.jvnet.maven.jellydoc.JellydocMojo.java
License:Apache License
public void generateSchema() throws MojoExecutionException { try {//from w ww .ja va 2s .c o m getLog().info("Generating XML Schema"); TransformerFactory tf = TransformerFactory.newInstance(); Templates templates = tf .newTemplates(new StreamSource(JellydocMojo.class.getResource("xsdgen.xsl").toExternalForm())); File source = new File(project.getBasedir(), "target/taglib.xml"); for (Element lib : (List<Element>) new SAXReader().read(source).selectNodes("/tags/library")) { String prefix = lib.attributeValue("prefix"); File schema = new File(project.getBasedir(), "target/taglib-" + prefix + ".xsd"); lib.getParent().remove(lib); // make it on its own DefaultDocument newDoc = new DefaultDocument(); newDoc.setRootElement(lib); templates.newTransformer().transform(new DocumentSource(newDoc), new StreamResult(new FileOutputStream(schema))); helper.attachArtifact(project, "xsd", "taglib-" + prefix, schema); } } catch (TransformerException e) { throw new MojoExecutionException("Failed to generate schema", e); } catch (DocumentException e) { throw new MojoExecutionException("Failed to generate schema", e); } catch (FileNotFoundException e) { throw new MojoExecutionException("Failed to generate schema", e); } }