分享

dom4j追加新节点换行输出

 开心豆豆2010 2013-03-26
使用dom4j解析xml文件时,新增加的节点通常会在一行中显示影像显示效果,下面的代码使用OutputFormat解决输出格式问题。
Document doc = DocumentHelper.parseText((String) message.getPayload());
            Element rootElt = doc.getRootElement();
            Element cdElement=rootElt.addElement("cd");
            Element titleE=cdElement.addElement("title");
            titleE.setText("星光礼赞");
            Element priceE=cdElement.addElement("price");
            priceE.setText("50");
          
            
            OutputFormat o = OutputFormat.createPrettyPrint();//new OutputFormat("  ", true, "UTF-8");
            //o.setIndent(" ");
            //o.setLineSeparator("/n");
            o.setEncoding("UTF-8");
            
            ByteArrayOutputStream a = new ByteArrayOutputStream(2048);//内存
            
            XMLWriter writer = new XMLWriter(a,o);
            try {
                writer.write(doc);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String payload = a.toString("UTF-8");//new String(buf, "UTF-8")
            
            message.setPayload(payload);//doc.asXML()

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多