像opDetail.addContent(makeDetailInfo("clearalarmNO",""));后面参数如果为空产生的是<fieldContent />想让生成的是<fieldContent></fieldContent>,代码如下,求解答。public String toSend(CurrentAlarm alarm, int alarmType, int sendType) {
org.jdom.Element opDetail = new org.jdom.Element("opDetail");
if (alarmType == 1){
opDetail.addContent(makeDetailInfo("alarmNO",alarm.getCuid()));
opDetail.addContent(makeDetailInfo("clearalarmNO",""));                  ...................private static org.jdom.Element makeDetailInfo(String fieldEnName, String fieldContent) {
        org.jdom.Element detailElement = new org.jdom.Element("fieldInfo");
        detailElement.addContent(new org.jdom.Element("fieldName").addContent(fieldEnName));
        detailElement.addContent(new org.jdom.Element("fieldContent").addContent(fieldContent+" "));
        return detailElement;
    }

解决方案 »

  1.   

    想要生成<fieldContent></fieldContent>这个节点吗?
      

  2.   

    Document doc = this.createSAXBuilder();
    //根节点
    Element roots = doc.getRootElement();
    //根节点的下一级节点
    Element root = roots.getChild(nodeName);
    //添加节点
    root.addContent(new Element(IConstants.childSubject));
    //添加节点属性 root.getChild(IConstants.childSubject).setAttribute(IConstants.childAttributeName,attributeValue);
    //添加节点内容
    root.getChild(IConstants.childSubject).setText(subject);
    root.addContent(new Element(IConstants.childContent)); root.getChild(IConstants.childContent).setAttribute(IConstants.childAttributeName,attributeValue);
    root.getChild(IConstants.childContent).setText(content);
    //寫入
    this.writerStr(doc);
    如果生成xml节点这样就可以了