java创建xml格式的字符串

java创建xml格式的字符串

下面直接贴代码:

package com.util.utils;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.springframework.stereotype.Service;


@Service("CreateXml")
public class CreateXml {


static int index=0;
public  static String createXmlFile(List list){

Element root = new Element("GT-5900");

Document doc = new Document(root);
Element newElement= null;
Element sonElement= null;
int flag=0;

Element DeviceInformation = new Element("DeviceInformation");
Element AlarmParameterRange = new Element("AlarmParameterRange");
Element IPAddress = new Element("IPAddress");
DeviceInformation.setAttribute("flag", "0");
AlarmParameterRange.setAttribute("flag", "0");
root.addContent(DeviceInformation);
root.addContent(AlarmParameterRange);
root.addContent(IPAddress);
List<String> machinelist = new java.util.ArrayList<String>();
machinelist.add("SysInfo");
machinelist.add("PreAmplifier");
machinelist.add("Amplifier");
machinelist.add("Amplifier");
machinelist.add("Amplifier");
machinelist.add("Amplifier");
List<String> machinesonlist = new java.util.ArrayList<String>();
machinesonlist.add("power,vswr,agcVoltage");
machinesonlist.add("power,currentA,temperature,acDcVoltageV1,acDcVoltageV2");
machinesonlist.add("power,currentA,temperature,acDcVoltage");
machinesonlist.add("power,currentA,temperature,acDcVoltage");
machinesonlist.add("power,currentA,temperature,acDcVoltage");
machinesonlist.add("power,currentA,temperature,acDcVoltage");
for(int i=0;i<machinelist.size();i++){
newElement=new  Element(machinelist.get(i));


        String valu=machinesonlist.get(i);
        String[] son=valu.split(",");
        
        if("Amplifier".equals(machinelist.get(i))){
        flag++;
        newElement.setAttribute("ID", flag+"");
        }
        for(int j=0;j<son.length;j++){
        sonElement=new  Element(son[j]);
        sonElement.setAttribute("value", "");
        newElement.addContent(sonElement);
     
     }
        
        DeviceInformation.addContent(newElement);
}

//运行日志400W以上 
Element dirPreAmplifier= new Element("PreAmplifier");

List<String> parentlist = new java.util.ArrayList<String>();
parentlist.add("dBmPower");
parentlist.add("current");
parentlist.add("temperature");
parentlist.add("acDcVoltageV1");
parentlist.add("acDcVoltageV2");

List<String> sonlist = new java.util.ArrayList<String>();
sonlist.add("lowPowerLower,lowPowerUpper,normalPowerLower,normalPowerUpper,highPowerLower,highPowerUpper");
sonlist.add("lowCurrentLower,lowCurrentUpper,normalCurrentLower,normalCurrentUpper,highCurrentLower,highCurrentUpper");
sonlist.add("lowTemperatureLower,lowTemperatureUpper,normalTemperatureLower,normalTemperatureUpper,highTemperatureLower,highTemperatureUpper");
sonlist.add("lowAcDcVoltageV1Lower,lowAcDcVoltageV1Upper,normalAcDcVoltageV1Lower,normalAcDcVoltageV1Upper,highAcDcVoltageV1Lower,highAcDcVoltageV1Upper");
sonlist.add("lowAcDcVoltageV2Lower,lowAcDcVoltageV2Upper,normalAcDcVoltageV2Lower,normalAcDcVoltageV2Upper,highAcDcVoltageV2Lower,highAcDcVoltageV2Upper");

 
for(int i=0;i<parentlist.size();i++){
newElement=new  Element(parentlist.get(i));
     newElement.setAttribute("flag", "0");


        String valu=sonlist.get(i);
        String[] son=valu.split(",");
        for(int j=0;j<son.length;j++){
        sonElement=new  Element(son[j]);
        sonElement.setAttribute("value", "");
        newElement.addContent(sonElement);
     
     }
     dirPreAmplifier.addContent(newElement);
}
Element Amplifiers= new Element("Amplifier");
 
List<String> parentlist2 = new java.util.ArrayList<String>();
parentlist2.add("dBmPower");
parentlist2.add("current");
parentlist2.add("temperature");
parentlist2.add("acDcVoltage");

List<String> sonlist2 = new java.util.ArrayList<String>();
sonlist2.add("lowPowerLower,lowPowerUpper,normalPowerLower,normalPowerUpper,highPowerLower,highPowerUpper");
sonlist2.add("lowCurrentLower,lowCurrentUpper,normalCurrentLower,normalCurrentUpper,highCurrentLower,highCurrentUpper");
sonlist2.add("lowTemperatureLower,lowTemperatureUpper,normalTemperatureLower,normalTemperatureUpper,highTemperatureLower,highTemperatureUpper");
sonlist2.add("lowAcDcVoltageLower,lowAcDcVoltageUpper,normalAcDcVoltageLower,normalAcDcVoltageUpper,highAcDcVoltageLower,highAcDcVoltageUpper");
for(int i=0;i<parentlist2.size();i++){
newElement=new  Element(parentlist2.get(i));
     newElement.setAttribute("flag", "0");


        String valu=sonlist2.get(i);
        String[] son=valu.split(",");
        for(int j=0;j<son.length;j++){
        sonElement=new  Element(son[j]);
        sonElement.setAttribute("value", "");
        newElement.addContent(sonElement);
     
     }
        Amplifiers.addContent(newElement);
}
Element systems= new Element("System");
List<String> parentlist3 = new java.util.ArrayList<String>();
parentlist3.add("dBmPower");
parentlist3.add("vswr");


List<String> sonlist3 = new java.util.ArrayList<String>();
sonlist3.add("lowPowerLower,lowPowerUpper,normalPowerLower,normalPowerUpper,highPowerLower,highPowerUpper");
sonlist3.add("lowVswrLower,lowVswrUpper,normalVswrLower,normalVswrUpper,highVswrLower,highVswrUpper");
for(int i=0;i<parentlist3.size();i++){
newElement=new  Element(parentlist3.get(i));
     newElement.setAttribute("flag", "0");


        String valu=sonlist3.get(i);
        String[] son=valu.split(",");
        for(int j=0;j<son.length;j++){
        sonElement=new  Element(son[j]);
        sonElement.setAttribute("value", "");
        newElement.addContent(sonElement);
     
     }
        systems.addContent(newElement);
}
List<String> ipsonlist = new java.util.ArrayList<String>();
ipsonlist.add("deviceIPAddress");
ipsonlist.add("deviceSubmask");
ipsonlist.add("deviceGateway");

for(int i=0;i<ipsonlist.size();i++){
sonElement=new  Element(ipsonlist.get(i));
        sonElement.setAttribute("value", "");
        IPAddress.addContent(sonElement);
}

//给xml进行赋值
if("1".equals(list.get(0))){
DeviceInformation.setAttribute("flag", "1");


// List<?> parentNote =DeviceInformation.getChildren();
// for(int  i=0;i<parentNote.size();i++){
// Element sonNote = (Element) parentNote.get(i);
// List<?> sonNoteList=sonNote.getChildren();
// for(int j=0;j<sonNoteList.size();j++){
// Element son=(Element) sonNoteList.get(j);
// son.setAttribute("value",(String) list.get(index));
// index++;
// }
// 
// }



}





//给PreAmplifier模块的xml赋值

if("1".equals(list.get(1))){
AlarmParameterRange.setAttribute("flag","1");
index=2;

List<?> alarmNote =dirPreAmplifier.getChildren();

for(int  i=0;i<alarmNote.size();i++){
Element sonNote = (Element) alarmNote.get(i);
sonNote.setAttribute("flag", "0");
List<?> sonalarmNote=sonNote.getChildren();
for(int j=0;j<sonalarmNote.size();j++){
Element son=(Element) sonalarmNote.get(j);
son.setAttribute("value",list.get(index).toString());
index++;
}

}

//给Amplifiers模块的xml赋值



Amplifiers.setAttribute("flag","0");


List<?> ampNote =Amplifiers.getChildren();

for(int  i=0;i<ampNote.size();i++){
Element sonNote = (Element) ampNote.get(i);
sonNote.setAttribute("flag", "0");
List<?> sonalarmNote=sonNote.getChildren();
for(int j=0;j<sonalarmNote.size();j++){
Element son=(Element) sonalarmNote.get(j);
son.setAttribute("value",list.get(index).toString());
index++;

}

}



//给system模块的xml赋值
systems.setAttribute("flag","0");

List<?> sysNote =systems.getChildren();

for(int  i=0;i<sysNote.size();i++){
Element sonNote = (Element) sysNote.get(i);
sonNote.setAttribute("flag","0");
List<?> sonalarmNote=sonNote.getChildren();
for(int j=0;j<sonalarmNote.size();j++){
Element son=(Element) sonalarmNote.get(j);
son.setAttribute("value",list.get(index).toString());
index++;
}

}



}else{
index=68;
}

List<?> ipNote =IPAddress.getChildren();

for(int i=0;i<ipNote.size();i++){
Element ipelement= (Element) ipNote.get(i);
String ipaddress=ipelement.getName().toString();
if("deviceIPAddress".equals(ipaddress)){

ipelement.setAttribute("value", list.get(index).toString());
}
}



AlarmParameterRange.addContent(dirPreAmplifier);
AlarmParameterRange.addContent(Amplifiers);
AlarmParameterRange.addContent(systems);
org.jdom.output.Format xmlFormat = org.jdom.output.Format
.getPrettyFormat();
xmlFormat.setEncoding("utf-8");
XMLOutputter out = new XMLOutputter(xmlFormat);
String sendInfo = out.outputString(doc).trim();
// System.out.println(sendInfo);
return sendInfo;



}

}
--------------------- 
作者:MRwanghongguang 
来源:CSDN 
原文:https://blog.csdn.net/mrwang1989/article/details/11714011 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页