Example usage for org.dom4j Document getRootElement

List of usage examples for org.dom4j Document getRootElement

Introduction

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

Prototype

Element getRootElement();

Source Link

Document

Returns the root Element for this document.

Usage

From source file:ch.epfl.codimsd.qep.QEPFactory.java

License:Open Source License

/**
 * Add an operator template to the QEP Document.
 * //from   w w  w .j ava  2s .c  om
 * @param document dom4j document.
 * @param opNode opNode structure of the operator to build.
 * @param opID identifier of the operator.
 */
private static void createOperator(Document document, OpNode opNode, int opID) {

    Element root = document.getRootElement();
    Iterator ittRoot = root.elementIterator();
    Element qepElement = (Element) ittRoot.next();

    // create new operator xml template (attributes, name)
    Element newOp = qepElement.addElement("op:Operator").addAttribute("id", opID + "").addAttribute("prod",
            buildProducers(opNode));

    if (opNode.getType() != null)
        newOp.addAttribute("type", opNode.getType());

    Element newNameOp = newOp.addElement("Name");
    newNameOp.addText(opNode.getOpName());

    // create operator xml parameters
    if (opNode.getParams() != null) {
        if (opNode.getParams().length != 0) {
            Element newParameterList = newOp.addElement("ParameterList");
            for (int i = 0; i < opNode.getParams().length; i++) {
                Element newParam = newParameterList.addElement("Param");
                newParam.addText(opNode.getParams()[i]);
            }
        }
    }
}

From source file:ch.javasoft.metabolic.efm.config.DistributedConfig.java

License:BSD License

private static final Element getDistConfigFromPackage() throws XmlConfigException, IOException {
    final String xmlName = "config/metabolic-efm.xml";
    InputStream xmlIn = DistributedConfig.class.getClassLoader().getResourceAsStream(xmlName);
    if (xmlIn == null) {
        throw new IOException("cannot find resource: " + xmlName);
    }//from   ww w  .  ja v  a2 s . c  o  m
    try {
        final SAXReader reader = new SAXReader();
        final Document doc = reader.read(xmlIn);
        final Element root = doc.getRootElement();
        final Element elDist = root.elementByID(XmlElement.distribute.getXmlName());
        //         final String defName = root.attributeValue("default", "default");
        //         final Element elConfig    = XmlUtil.getChildElementByAttributeValue(root, ch.javasoft.xml.config.XmlConfig.XmlElement.config, XmlAttribute.name, defName, true /*throwExceptionIfNull*/);
        //         final Element elMetEfm    = XmlUtil.getRequiredSingleChildElement(elConfig, ch.javasoft.metabolic.efm.main.CalculateFluxModes.XmlElement.metabolic_efm);
        //         final Element elImpl      = XmlUtil.getRequiredSingleChildElement(elMetEfm, ch.javasoft.metabolic.efm.main.CalculateFluxModes.XmlElement.efm_impl);
        //         final Element elImplCfg   = XmlUtil.getRequiredSingleChildElement(elImpl, ch.javasoft.metabolic.efm.main.CalculateFluxModes.XmlElement.config);
        //         final Element elDist      = XmlUtil.getRequiredSingleChildElement(elImplCfg, XmlElement.distribute);
        return elDist;
    } catch (DocumentException e) {
        throw ExceptionUtil.toIOException("cannot parse " + xmlName + ", e=" + e, e);
    } finally {
        xmlIn.close();
    }
}

From source file:ch.javasoft.metabolic.parse.SbmlParser.java

License:BSD License

public MetabolicNetwork parse(Document sbml, String modelName) throws DocumentException {
    Element root = sbml.getRootElement();
    Element model;/*from  w w w  .  j a va2 s  .  c om*/
    if (modelName == null) {
        model = root.element(ELEMENT_MODEL);
    } else {
        Node node = root.selectSingleNode("model[@name=\"" + modelName + "\"]");
        if (node instanceof Element) {
            model = (Element) node;
        } else {
            throw new IllegalArgumentException("model not found: " + modelName);
        }
    }
    Map<Annotateable, Map<String, String>> annotations = new LinkedHashMap<Annotateable, Map<String, String>>();
    Map<String, CompartmentMetabolite> metas = parseMetabolites(model.element(ELEMENT_METABOLITES),
            annotations);
    Set<CompartmentReaction> reacts = parseReactions(model.element(ELEMENT_REACTIONS), metas, annotations);
    return createNetwork(model, metas, reacts, annotations);
}

From source file:ch.javasoft.xml.config.XmlConfig.java

License:BSD License

/**
 * Returns a copy of the underlying xml document, including main arguments 
 * if any have been specified. From the returned document, 
 *//*  w ww .j  ava  2 s  .co  m*/
public Document toXmlDocument() {
    final Document doc = (Document) mDocument.clone();
    doc.getRootElement().elements(XmlElement.application.getXmlName()).clear();
    final Element app = doc.getRootElement().addElement(XmlElement.application.getXmlName());
    app.addAttribute(XmlAttribute.name.getXmlName(), mAppName);
    for (String arg : mArgs) {
        final Element elArg = app.addElement(XmlElement.arg.getXmlName());
        elArg.addAttribute(XmlAttribute.value.getXmlName(), arg);
    }
    return doc;
}

From source file:ch.javasoft.xml.config.XmlConfig.java

License:BSD License

/**
 * Parses the given document and returns an XmlConfig instance. Note that
 * this document is expected to contain an application element (direct child
 * of root element) with a name attribute and possible with arg child nodes.
 * Such a document is usually received by calling {@link #toXmlDocument()}.
 *//*www. j av a  2  s  .c o m*/
@SuppressWarnings("unchecked")
public static XmlConfig fromXmlDocument(Document doc) {
    final Element app = doc.getRootElement().element(XmlElement.application.getXmlName());
    final String appName = app.attributeValue(XmlAttribute.name.getXmlName());
    final List<String> args = new ArrayList<String>();
    final Iterator<Element> it = app.elementIterator(XmlElement.arg.getXmlName());
    while (it.hasNext()) {
        args.add(it.next().attributeValue(XmlAttribute.value.getXmlName()));
    }
    doc.remove(app);
    return new XmlConfig(appName, doc, args.toArray(new String[args.size()]));
}

From source file:ch.javasoft.xml.config.XmlConfig.java

License:BSD License

public Element getConfigElement(String name) throws XmlConfigException {
    final Document doc = getConfigDocument(name);
    final Element root = doc.getRootElement();
    return XmlUtil.getRequiredSingleChildElement(root, XmlElement.config);
}

From source file:ch.javasoft.xml.config.XmlPrint.java

License:BSD License

/**
 * Print the given document using the given print writer
 * @param doc       the xml document/*ww w .j  ava 2 s .co m*/
 * @param writer   the print writer to use for the output
 */
public void print(Document doc, PrintWriter writer) {
    printDeclaration(doc, writer);
    print(doc.getRootElement(), writer);
}

From source file:cmcc.gz.adc.message.CorpBindReq.java

@SuppressWarnings("unchecked")
public CorpBindReq(String msg) throws Exception {
    XmlParseTool xml = new XmlParseTool(msg);

    //??/*from   w  w w . j  a  v a 2  s  . c  o  m*/
    this.header = xml.getHeader();

    //??
    String bodyText;
    if (null == (bodyText = xml.getBodyText())) {
        throw new Exception("CorpBindReq??");
    }

    try {
        Document document = DocumentHelper.parseText(bodyText);
        Element root = document.getRootElement();
        this.corpName = root.element("CORPNAME").getTextTrim();
        this.corpAccount = root.element("CORPACCOUNT").getTextTrim();
        this.license = root.element("LICENSE").getTextTrim();
        this.optype = root.element("OPTYPE").getTextTrim();
        this.opnote = root.element("OPNOTE").getTextTrim();

        //PARAMMAP
        paramMap = new HashMap<String, String>();
        if (null != root.element("PARAMLIST").element("PARAMMAP")) {
            for (Iterator<Element> it = root.element("PARAMLIST").elementIterator("PARAMMAP"); it.hasNext();) {
                Element e = it.next();
                String name = e.element("PARAMNAME").getTextTrim();
                String value = e.element("PARAMVALUE").getTextTrim();
                if (0 < name.length() && 0 < value.length())
                    paramMap.put(name, value);
            }
        }

        //CORPINFOMAP
        corpInfoMap = new HashMap<String, String>();
        if (null != root.element("CORPINFOLIST").element("CORPINFOMAP")) {
            for (Iterator<Element> it = root.element("CORPINFOLIST").elementIterator("CORPINFOMAP"); it
                    .hasNext();) {
                Element e = it.next();
                String name = e.element("CORPINFONAME").getTextTrim();
                String value = e.element("CORPINFOVALUE").getTextTrim();
                if (0 < name.length() && 0 < value.length()) {
                    corpInfoMap.put(name, value);

                    if ("CORP_SHORTNAME".equals(name))
                        this.corpShortName = value;
                    else if ("CORP_LINKMAN".equals(name))
                        this.corpLinkMan = value;
                    else if ("CORP_LINKPHONE".equals(name))
                        this.corpLinkPhone = value;
                    else if ("CORP_LINKMOBILE".equals(name))
                        this.corpLinkMobile = value;
                }
            }
        }

        //ORDERPOINTMAP
        orderPointMap = new HashMap<String, String>();
        int count = 1;
        if (null != root.element("POINTLIST").element("ORDERPOINTMAP")) {
            for (Iterator<Element> it = root.element("POINTLIST").elementIterator("ORDERPOINTMAP"); it
                    .hasNext();) {
                Element e = it.next();
                String name = e.element("POINTNAME").getTextTrim();
                String value = e.element("POINTVALUE").getTextTrim();
                if (0 < name.length() && 0 < value.length()) {
                    orderPointMap.put(name, value);
                    this.point = (count == 1) ? name : String.format("%s,%s", point, name);
                    count++;
                }
            }
        }
    } catch (Exception ex) {
        throw new Exception(ex.getMessage());
    }
}

From source file:cn.b2b.index.product.create.ProductDataFromDB.java

private String getBrand(String xml) {
    Document doc;
    try {//  w w w  .ja  v  a 2  s .c  o m
        doc = DocumentHelper.parseText(xml);

        Element rootNode = doc.getRootElement();
        Iterator iter = rootNode.nodeIterator();
        while (iter.hasNext()) {
            Element recordEle = (Element) iter.next();
            String title = recordEle.getName();
            if (title.equals("P1")) {
                return recordEle.getText();
            }
            return "";
        }
    } catch (DocumentException e) {
        // e.printStackTrace();
    }
    return "";
}

From source file:cn.b2b.index.product.create.ProductDataFromDB.java

private String getItemByXml(String xml, String nodeName) {
    Document doc;
    try {//from   ww w.j  a  v  a2s  .  c om
        doc = DocumentHelper.parseText(xml);

        Element rootNode = doc.getRootElement();
        Iterator iter = rootNode.nodeIterator();
        while (iter.hasNext()) {
            Element recordEle = (Element) iter.next();
            String title = recordEle.getName();
            if (title.equals(nodeName)) {
                return recordEle.getText();
            }
            return "";
        }
    } catch (DocumentException e) {
        // e.printStackTrace();
    }
    return "";
}