Example usage for org.dom4j Document addElement

List of usage examples for org.dom4j Document addElement

Introduction

In this page you can find the example usage for org.dom4j Document addElement.

Prototype

Element addElement(String name);

Source Link

Document

Adds a new Element node with the given name to this branch and returns a reference to the new node.

Usage

From source file:org.apache.openmeetings.servlet.outputhandler.LangExport.java

License:Apache License

public static Element createRoot(Document document) {
    Element root = document.addElement("language");
    root.add(new Namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"));
    root.add(new Namespace("noNamespaceSchemaLocation", "language.xsd"));
    return root;/*  w ww.j  a v  a  2  s .c o  m*/
}

From source file:org.apache.openmeetings.util.LangExport.java

License:Apache License

public static Element createRoot(Document document) {
    Element root = document.addElement("language");
    Namespace xsi = new Namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.add(xsi);/*from ww  w.  j a v a  2s  . co  m*/
    root.add(new FlyweightAttribute("noNamespaceSchemaLocation", "language.xsd", xsi));
    return root;
}

From source file:org.apache.openmeetings.util.XmlExport.java

License:Apache License

public static Element createRoot(Document document) {
    document.addDocType("properties", null, "http://java.sun.com/dtd/properties.dtd");
    Element root = document.addElement("properties");
    return root;//from w ww . j  a  v  a 2s . c  o m
}

From source file:org.apache.openmeetings.util.XmlExport.java

License:Apache License

public static Element createRoot(Document document, String _root) {
    Element root = document.addElement(_root);
    return root;// ww w.j ava  2  s.  c om
}

From source file:org.apache.poi.openxml4j.opc.internal.ContentTypeManager.java

License:Apache License

/**
 * Save the contents type part./*w  w  w .  j av a  2 s .c om*/
 *
 * @param outStream
 *            The output stream use to save the XML content of the content
 *            types part.
 * @return <b>true</b> if the operation success, else <b>false</b>.
 */
public boolean save(OutputStream outStream) {
    Document xmlOutDoc = DocumentHelper.createDocument();

    // Building namespace
    Namespace dfNs = Namespace.get("", TYPES_NAMESPACE_URI);
    Element typesElem = xmlOutDoc.addElement(new QName(TYPES_TAG_NAME, dfNs));

    // Adding default types
    for (Entry<String, String> entry : defaultContentType.entrySet()) {
        appendDefaultType(typesElem, entry);
    }

    // Adding specific types if any exist
    if (overrideContentType != null) {
        for (Entry<PackagePartName, String> entry : overrideContentType.entrySet()) {
            appendSpecificTypes(typesElem, entry);
        }
    }
    xmlOutDoc.normalize();

    // Save content in the specified output stream
    return this.saveImpl(xmlOutDoc, outStream);
}

From source file:org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller.java

License:Apache License

/**
 * Save relationships into the part.//  ww w  . j  a v  a2s  .  c  o m
 *
 * @param rels
 *            The relationships collection to marshall.
 * @param relPartName
 *            Part name of the relationship part to marshall.
 * @param zos
 *            Zip output stream in which to save the XML content of the
 *            relationships serialization.
 */
public static boolean marshallRelationshipPart(PackageRelationshipCollection rels, PackagePartName relPartName,
        ZipOutputStream zos) {
    // Building xml
    Document xmlOutDoc = DocumentHelper.createDocument();
    // make something like <Relationships
    // xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    Namespace dfNs = Namespace.get("", PackageNamespaces.RELATIONSHIPS);
    Element root = xmlOutDoc.addElement(new QName(PackageRelationship.RELATIONSHIPS_TAG_NAME, dfNs));

    // <Relationship
    // TargetMode="External"
    // Id="rIdx"
    // Target="http://www.custom.com/images/pic1.jpg"
    // Type="http://www.custom.com/external-resource"/>

    URI sourcePartURI = PackagingURIHelper.getSourcePartUriFromRelationshipPartUri(relPartName.getURI());

    for (PackageRelationship rel : rels) {
        // the relationship element
        Element relElem = root.addElement(PackageRelationship.RELATIONSHIP_TAG_NAME);

        // the relationship ID
        relElem.addAttribute(PackageRelationship.ID_ATTRIBUTE_NAME, rel.getId());

        // the relationship Type
        relElem.addAttribute(PackageRelationship.TYPE_ATTRIBUTE_NAME, rel.getRelationshipType());

        // the relationship Target
        String targetValue;
        URI uri = rel.getTargetURI();
        if (rel.getTargetMode() == TargetMode.EXTERNAL) {
            // Save the target as-is - we don't need to validate it,
            //  alter it etc
            targetValue = uri.toString();

            // add TargetMode attribute (as it is external link external)
            relElem.addAttribute(PackageRelationship.TARGET_MODE_ATTRIBUTE_NAME, "External");
        } else {
            URI targetURI = rel.getTargetURI();
            targetValue = PackagingURIHelper.relativizeURI(sourcePartURI, targetURI, true).toString();
        }
        relElem.addAttribute(PackageRelationship.TARGET_ATTRIBUTE_NAME, targetValue);
    }

    xmlOutDoc.normalize();

    // String schemaFilename = Configuration.getPathForXmlSchema()+
    // File.separator + "opc-relationships.xsd";

    // Save part in zip
    ZipEntry ctEntry = new ZipEntry(
            ZipHelper.getZipURIFromOPCName(relPartName.getURI().toASCIIString()).getPath());
    try {
        zos.putNextEntry(ctEntry);
        if (!StreamHelper.saveXmlInStream(xmlOutDoc, zos)) {
            return false;
        }
        zos.closeEntry();
    } catch (IOException e) {
        logger.log(POILogger.ERROR, "Cannot create zip entry " + relPartName, e);
        return false;
    }
    return true; // success
}

From source file:org.apache.taglibs.xtags.servlet.XPathServlet.java

License:Apache License

protected Document createDocument(HttpServletRequest request) throws ServletException {
    Document document = DocumentHelper.createDocument();
    Element element = document.addElement("results");

    try {//from   w w  w.j ava  2 s  .  c o m
        URL url = getDocumentURL(request);
        if (url != null) {
            String path = request.getParameter("path");
            if (path != null && path.length() > 0) {
                Document source = reader.read(url);
                XPath xpath = source.createXPath(path);

                String contextPath = request.getParameter("contextPath");
                if (contextPath == null) {
                    contextPath = ".";
                }
                List context = source.selectNodes(contextPath);

                List results = null;
                if (!getBoolean(request, "sort")) {
                    results = xpath.selectNodes(context);
                } else {
                    String sortPath = request.getParameter("sortPath");
                    if (sortPath == null) {
                        sortPath = ".";
                    }
                    boolean distinct = getBoolean(request, "distinct");
                    XPath sortXPath = source.createXPath(sortPath);
                    results = xpath.selectNodes(context, sortXPath, distinct);
                }
                appendResults(element, results);
            }
        }
    } catch (DocumentException e) {
        e.printStackTrace();
        throw new ServletException("Error parsing document: " + e, e);
    }
    return document;
}

From source file:org.apereo.portal.io.xml.dlm.FragmentDefinitionExporter.java

License:Apache License

@Override
protected Element exportDataElement(String id) {
    final FragmentDefinition fragmentDefinition = fragmentDefinitionDao.getFragmentDefinition(id);
    if (fragmentDefinition == null) {
        return null;
    }//from   ww  w  .java 2 s.  c  o m

    final org.dom4j.Document fragmentDefDoc = new org.dom4j.DocumentFactory().createDocument();
    final Element fragmentDefElement = fragmentDefDoc.addElement("fragment-definition");
    fragmentDefElement.addNamespace("dlm", Constants.NS_URI);
    fragmentDefElement.addAttribute("script",
            "classpath://org/jasig/portal/io/import-fragment-definition_v3-1.crn");
    fragmentDefinition.toElement(fragmentDefElement);

    return fragmentDefElement;
}

From source file:org.apereo.portal.io.xml.pags.PersonAttributesGroupStoreExporter.java

License:Apache License

@Override
protected Element exportDataElement(String name) {
    final Set<IPersonAttributesGroupDefinition> personAttributesGroupDefinitions = personAttributesGroupDefinitionDao
            .getPersonAttributesGroupDefinitionByName(name);

    if (personAttributesGroupDefinitions.isEmpty()) {
        return null;
    }// ww w  .  j  a v a  2  s .c o m

    final org.dom4j.Document pagsGroupDefDoc = new org.dom4j.DocumentFactory().createDocument();
    final Element pagsGroupDefElement = pagsGroupDefDoc.addElement("pags-group");
    pagsGroupDefElement.addAttribute("script", "classpath://org/jasig/portal/io/import-pags-group_v4-1.crn");
    personAttributesGroupDefinitions.iterator().next().toElement(pagsGroupDefElement);

    return pagsGroupDefElement;
}

From source file:org.arangitester.log.XmlResult.java

License:Apache License

public void save(File file, FunctionalSuite result) {

    // file indicates a pathname
    Document document = DocumentHelper.createDocument(); // create a new xml document
    Element root = document.addElement("LccFunctionalSuite"); // create the root tag named LccFunctionalSuite
    if (result == null)
        return;//www .  ja v  a  2  s .c o  m

    SummaryHelper summary = new SummaryHelper(result);

    root.addAttribute("totalTime", String.valueOf(summary.getTotalTime()));
    System.out.println("Tempo de Execuo: " + summary.getTotalTime() + " min");
    root.addAttribute("total", String.valueOf(summary.getTotal()));
    System.out.println("Total: " + summary.getTotal());
    root.addAttribute("skip", String.valueOf(summary.getSkip()));
    System.out.println("Skiped: " + summary.getSkip());
    root.addAttribute("fail", String.valueOf(summary.getFail()));
    System.out.println("Fail: " + summary.getFail());
    root.addAttribute("successful", String.valueOf(summary.getSuccessful()));
    System.out.println("Successful: " + summary.getSuccessful());
    root.addAttribute("percent", String.valueOf(summary.getPercent()));
    System.out.println("Sucessful: " + summary.getPercent() + "%");

    for (UseCase usecase : result.getCases()) {
        Element userCaseElement = root.addElement("UseCase").addAttribute("name", usecase.getName())
                .addAttribute("startTime", getFormatedDate((usecase.getStartTime())))
                .addAttribute("endTime", getFormatedDate(usecase.getEndTime()));

        for (String obs : usecase.getObs()) {
            Element obsElement = userCaseElement.addElement("Obs");
            obsElement.addText(obs);
        }

        for (Object log : usecase.getlogs()) {
            if (log instanceof Info) {
                userCaseElement.addElement("info").addText(((Info) log).getMessage());
            } else {
                Error error = ((Error) log);
                Element errorElement = userCaseElement.addElement("error").addAttribute("cause",
                        error.getCause());
                if (error.getScreenshot() != null)
                    errorElement.addAttribute("screenshot", error.getScreenshot());

                if (error.getError() != null)
                    errorElement.addText(error.getError());
            }
        }

        for (TestCase testcase : usecase.getTestcases()) {
            Element testCaseElement = userCaseElement.addElement("TestCase")
                    .addAttribute("name", testcase.getJavaMethod())
                    .addAttribute("description", testcase.getTestcase())
                    .addAttribute("startTime", getFormatedDate(testcase.getStartTime()))
                    .addAttribute("endTime", getFormatedDate(testcase.getEndTime()))
                    .addAttribute("skip", String.valueOf(testcase.isSkip()));

            for (Object log : testcase.getlogs()) {
                if (log instanceof Info) {
                    testCaseElement.addElement("info").addText(((Info) log).getMessage());
                } else {
                    Error error = ((Error) log);
                    Element errorElement = testCaseElement.addElement("error").addAttribute("cause",
                            error.getCause());
                    if (error.getScreenshot() != null)
                        errorElement.addAttribute("screenshot", error.getScreenshot());

                    if (error.getError() != null)
                        errorElement.addText(error.getError());
                }
            }

        }
    }
    // End of the document building
    // Now, we will start to write in document
    try {
        file.getParentFile().mkdirs();
        file.createNewFile();
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        XMLWriter write = new XMLWriter(new FileWriter(file), outformat); // Initialize the xml file
        write.write(document); // Write the final document on xml file
        write.close();
    } catch (IOException e) {
        System.out.println("Erro durante a gravao no arquivo " + file + " :\n" + e.toString());
    }

}