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:com.rdvmedecin.proprietes.Properties.java

public static void main(String args[]) throws DocumentException {
    File fichier = new File("../../configuration.xml");
    SAXReader reader = new SAXReader();
    Document doc = reader.read(fichier);
    Element root = doc.addElement("root");
    /*/*from   ww w.j  a va2  s. c  om*/
    Element author2 = root.addElement( "author" )
    .addAttribute( "id", "Toby" ).addAttribute( "location", "Germany" )
    .addText( "Tobias Rademacher" );
    Element author1 = root.addElement( "author" )
    .addAttribute( "id", "James" ).addAttribute( "location", "UK" )
    .addText( "James Strachan" );
    */
    List results = doc.selectNodes("//author[@location = 'UK']");
    for (Iterator iter = results.iterator(); iter.hasNext();) {
        Element element = (Element) iter.next();
        System.out.println(element.valueOf("concat(@id,' : ', .)"));
    }
}

From source file:com.rowtheboat.gui.OptionsSingleton.java

License:Open Source License

/**
 * This method saves the options to the options.xml file
 *///w w  w  .j av  a2  s . c om
public void saveOptions() throws SAXException, IOException {

    /* Start the document */
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(new FileWriter(optionsFile), format);
    writer.startDocument();

    /* Add the main options section */
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("Options");

    /* Standard options */
    DefaultElement standardElement = new DefaultElement("Standard");
    standardElement.addElement("FullStrokeData").addText(getFullStrokeData() + "");
    standardElement.addElement("BoatSmoothing").addText(getBoatSmoothing() + "");
    root.add(standardElement);

    /* Input options */
    DefaultElement inputElement = new DefaultElement("Input");
    inputElement.addElement("SerialPort").addText(getSerialPort());
    root.add(inputElement);

    /* Race options */
    DefaultElement raceElement = new DefaultElement("Race");
    raceElement.addElement("Countdown").addText(getDelay() + "");
    root.add(raceElement);

    /* End the document */
    writer.write(root);
    writer.endDocument();
    writer.close();
}

From source file:com.sammyun.util.XmlHelper.java

License:Open Source License

/**
 * Dto??XML?(?)/*  w ww  .j  av a  2 s . c  o m*/
 * 
 * @param dto Dto
 * @param pRootNodeName ??
 * @return string XML?
 */
public static final String parseDto2Xml(Dto pDto, String pRootNodeName) {
    if (EduUtil.isEmpty(pDto)) {
        logger.warn("DTO,");
        return "<root />";
    }
    Document document = DocumentHelper.createDocument();
    // 
    document.addElement(pRootNodeName);
    Element root = document.getRootElement();
    Iterator keyIterator = pDto.keySet().iterator();
    while (keyIterator.hasNext()) {
        String key = (String) keyIterator.next();
        String value = pDto.getAsString(key);
        Element leaf = root.addElement(key);
        leaf.setText(value);
    }
    // XML?
    String outXml = document.asXML().substring(39);
    return outXml;
}

From source file:com.sammyun.util.XmlHelper.java

License:Open Source License

/**
 * Dto??XML?(?)/*from  ww w  .  j  ava  2 s .c o  m*/
 * 
 * @param pDto Dto
 * @param pRootNodeName ??
 * @param pFirstNodeName ??
 * @return string XML?
 */
public static final String parseDto2Xml(Dto pDto, String pRootNodeName, String pFirstNodeName) {
    if (EduUtil.isEmpty(pDto)) {
        logger.warn("DTO,");
        return "<root />";
    }
    Document document = DocumentHelper.createDocument();
    // 
    document.addElement(pRootNodeName);
    Element root = document.getRootElement();
    root.addElement(pFirstNodeName);
    Element firstEl = (Element) document.selectSingleNode("/" + pRootNodeName + "/" + pFirstNodeName);
    Iterator keyIterator = pDto.keySet().iterator();
    while (keyIterator.hasNext()) {
        String key = (String) keyIterator.next();
        String value = pDto.getAsString(key);
        firstEl.addAttribute(key, value);
    }
    // XML?
    String outXml = document.asXML().substring(39);
    return outXml;
}

From source file:com.sammyun.util.XmlHelper.java

License:Open Source License

/**
 * List???XML?(?)/*from  www  . j  av  a  2 s . co m*/
 * 
 * @param pList List?(List?Dto?VO?Domain)
 * @param pRootNodeName ??
 * @param pFirstNodeName ??
 * @return string XML?
 */
public static final String parseList2Xml(List pList, String pRootNodeName, String pFirstNodeName) {
    Document document = DocumentHelper.createDocument();
    Element elRoot = document.addElement(pRootNodeName);
    for (int i = 0; i < pList.size(); i++) {
        Dto dto = (Dto) pList.get(i);
        Element elRow = elRoot.addElement(pFirstNodeName);
        Iterator it = dto.entrySet().iterator();
        while (it.hasNext()) {
            Dto.Entry entry = (Dto.Entry) it.next();
            elRow.addAttribute((String) entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    String outXml = document.asXML().substring(39);
    return outXml;
}

From source file:com.sammyun.util.XmlHelper.java

License:Open Source License

/**
 * List???XML?(?)/*from  w  w w  . ja  v a  2  s .  c  om*/
 * 
 * @param pList List?(List?Dto?VO?Domain)
 * @param pRootNodeName ??
 * @param pFirstNodeName ??
 * @return string XML?
 */
public static final String parseList2XmlBasedNode(List pList, String pRootNodeName, String pFirstNodeName) {
    Document document = DocumentHelper.createDocument();
    Element output = document.addElement(pRootNodeName);
    for (int i = 0; i < pList.size(); i++) {
        Dto dto = (Dto) pList.get(i);
        Element elRow = output.addElement(pFirstNodeName);
        Iterator it = dto.entrySet().iterator();
        while (it.hasNext()) {
            Dto.Entry entry = (Dto.Entry) it.next();
            Element leaf = elRow.addElement((String) entry.getKey());
            leaf.setText(String.valueOf(entry.getValue()));
        }
    }
    String outXml = document.asXML().substring(39);
    return outXml;
}

From source file:com.secuotp.model.xml.XMLCreate.java

public static Document createResponseXML(int error, String service, String message) {
    Document d = new DefaultDocument();

    Element root = d.addElement("secuotp").addAttribute("status", "" + error);
    root.addElement("service").addText(service);
    root.addElement("message").addText(message);

    d.normalize();//from ww w .  j  a va  2s . co  m
    return d;
}

From source file:com.secuotp.model.xml.XMLCreate.java

public static Document createResponseXMLWithData(String service, String message, XMLParameter param) {
    Document d = new DefaultDocument();

    Element root = d.addElement("secuotp").addAttribute("status", "101");
    root.addElement("service").addText(service);
    root.addElement("message").addText(message);
    Element responseNode = root.addElement("response");
    while (param.hasNext()) {
        String[] parameter = param.pop();
        responseNode.addElement(parameter[0]).setText(parameter[1]);
    }//from  w  w  w . j  a v a2 s  . c o m

    d.normalize();
    return d;
}

From source file:com.sf.integration.warehouse.service.SFService.java

/**
 * ?()//from  w ww  .  j a v  a 2  s  .c om
 * @param dctx
 * @param context
 * @return
 */
@SuppressWarnings("unchecked")
public static Map<String, Object> querySFProductBySku(DispatchContext dctx, Map<String, Object> context) {

    Document doc = (Document) DocumentHelper.createDocument();
    //Request
    Element request = doc.addElement("Request");
    request.addAttribute("service", "ITEM_QUERY_SERVICE");
    request.addAttribute("lang", "zh-CN");
    //Head
    Element head = request.addElement("Head");
    Element AccessCode = head.addElement("AccessCode");
    AccessCode.setText(ACCESS_CODE);//?
    Element Checkword = head.addElement("Checkword");
    Checkword.setText(CHECK_WORD);//??
    //Body
    Element Body = request.addElement("Body");
    Element ItemQueryRequest = Body.addElement("ItemQueryRequest");
    Element CompanyCode = ItemQueryRequest.addElement("CompanyCode");
    CompanyCode.setText(COMPANY_CODE);//?
    Element SkuNoList = ItemQueryRequest.addElement("SkuNoList");
    List<String> skuNoList = (List<String>) context.get("skuNoList");
    for (String skuNo : skuNoList) {
        Element SkuNo = SkuNoList.addElement("SkuNo");
        SkuNo.setText(skuNo);//??
    }
    String result = "";
    String XMLStr = doc.getRootElement().asXML();
    try {
        result = sendSFRequest(XMLStr);

    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return ServiceUtil.returnSuccess(result);
}

From source file:com.sf.integration.warehouse.service.SFService.java

/**
 * ??()/*from w w  w.j  a  v a 2 s .c om*/
 * @param dctx
 * @param context
 * @return
 */
@SuppressWarnings("unchecked")
public static Map<String, Object> SFProductSync(DispatchContext dctx, Map<String, Object> context) {
    Map<String, Object> result = ServiceUtil.returnSuccess();
    Delegator delegator = dctx.getDelegator();
    String message = verificationProductSyncParameters(context);
    if (!message.equals("success")) {
        return ServiceUtil.returnError(message);
    }
    Document doc = (Document) DocumentHelper.createDocument();
    //Request
    Element request = doc.addElement("Request");
    request.addAttribute("service", "ITEM_SERVICE");
    request.addAttribute("lang", "zh-CN");
    //Head
    Element head = request.addElement("Head");
    Element AccessCode = head.addElement("AccessCode");
    AccessCode.setText(ACCESS_CODE);//?
    Element Checkword = head.addElement("Checkword");
    Checkword.setText(CHECK_WORD);//??
    //Body
    Element Body = request.addElement("Body");
    Element ItemRequest = Body.addElement("ItemRequest");

    Element CompanyCode = ItemRequest.addElement("CompanyCode");
    CompanyCode.setText(COMPANY_CODE);//?
    Element Items = ItemRequest.addElement("Items");
    for (Map<String, Object> productItem : (List<Map<String, Object>>) context.get("items")) {
        Element Item = Items.addElement("Item");

        Element SkuNo = Item.addElement("SkuNo");
        if (UtilValidate.isNotEmpty(productItem.get("skuNo"))) {
            SkuNo.setText(productItem.get("skuNo").toString());//??
        }
        Element ItemName = Item.addElement("ItemName");
        if (UtilValidate.isNotEmpty(productItem.get("itemName"))) {
            ItemName.setText(productItem.get("itemName").toString());//???
        }
        Element ItemColor = Item.addElement("ItemColor");
        if (UtilValidate.isNotEmpty(productItem.get("itemColor"))) {
            ItemColor.setText(productItem.get("itemColor").toString());//
        }
        Element ItemSize = Item.addElement("ItemSize");
        if (UtilValidate.isNotEmpty(productItem.get("itemSize"))) {
            ItemSize.setText(productItem.get("itemSize").toString());//?
        }
        Element Containers = Item.addElement("Containers");
        Element Container = Containers.addElement("Container");

        Element PackUm = Container.addElement("PackUm");
        if (UtilValidate.isNotEmpty(productItem.get("packUm"))) {//??
            PackUm.setText(productItem.get("packUm").toString());
        }
        Element UmDescr = Container.addElement("UmDescr");
        if (UtilValidate.isNotEmpty(productItem.get("umDescr"))) {//??
            UmDescr.setText(productItem.get("umDescr").toString());
        }
    }
    String XMLStr = doc.getRootElement().asXML();

    try {
        String resultStr = sendSFRequest(XMLStr);
        Document resultDocument = (Document) DocumentHelper.parseText(resultStr);
        Element ResponseElement = resultDocument.getRootElement();
        Element HeadElement = ResponseElement.element("Head");
        SFAuditLogFilter.saveRequestContext(delegator, "ITEM_SERVICE", XMLStr, resultStr);
        if (HeadElement.getText().equals("ERR")) {
            Element ErrorElement = ResponseElement.element("Error");
            String code = ErrorElement.attributeValue("code");
            String errorMessage = ErrorElement.getText();
            return ServiceUtil.returnError("ERROR : errorCode={" + code + "} message={" + errorMessage + "}");
        } else {
            Element BodyElement = ResponseElement.element("Body");
            Element ItemResponseElement = BodyElement.element("ItemResponse");
            Element ItemsElement = ItemResponseElement.element("Items");
            Element ItemElement = ItemsElement.element("Item");
            Element ResultElement = ItemElement.element("Result");
            result.put("resultCode", ResultElement.getText());
            return result;
        }
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return ServiceUtil.returnError(e.getMessage());
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return ServiceUtil.returnError(e.getMessage());
    }
}