你们中的许多人都可以帮我解决这个问题。我在stackoverflow上经历了很多问题的答案,但是在某种程度上,我编写的代码似乎并不像我希望的那样工作。
任何帮助将不胜感激。
我也看到了这篇优秀的文章http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html,它向我解释了很多东西。
我尝试实现它但没有获得所需的xml格式。任何帮助都感激不尽。 谢谢。
下面我发布了代码:
我的课程有两个课程: DSC.java和Identity.java,前一类通过调用Identity类中的get / set方法来设置值。
:::::::::: Identity.java ::::::::::::
@XmlRootElement(name="LabbuddyArray")
public class Identity {
public String toString()
{
return "DSC_XML_OUTPUT [Company_name=" + company_name + ", Model_Number=" + model_number + ", Serial_Number=" + serial_number + ", New_BIAS=" + new_bias +", New_TEMP=" + new_temp + "]";
}
//DECLARE VARIABLES
String company_name;
String model_number;
String serial_number;
String port_number;
float photo_current;
float actual_bias;
float new_bias;
float actual_temp;
float new_temp;
List<String> slots;
public Identity(){}
//DECLARING CLASSES FOR XML FORMATTING
//GETTING AND SETTING SLOTS TO XML
@XmlElementWrapper
@XmlElement(name="slot")
public List<String> getSlots (){
return slots;
}
public void setSlots (List<String> slots){
this.slots = slots;
}
public String getIdentityCompanyName() {
return company_name;
}
@XmlElement(name="setIdentityCompanyName")
public void setIdentityCompanyName(String identity_company_name) {
this.company_name = identity_company_name;
}
//GETTING AND SETTING MODEL_NUMBER TO XML
public String getIdentityModelNumber() {
return model_number;
}
@XmlElement(name="setIdentityModelNumber")
public void setIdentityModelNumber(String model_number) {
this.model_number = model_number;
}
//GETTING AND SETTING SERIAL_NUMBER TO XML
public String getIdentitySerialNumber() {
return serial_number;
}
@XmlElement(name="setIdentitySerialNumber")
public void setIdentitySerialNumber(String serial_number) {
this.serial_number = serial_number;
}
//GETTING AND SETTING PORT_NUMBER TO XML
public String getIdentityPortNumber() {
return port_number;
}
@XmlElement (name="setIdentityPortNumber")
public void setIdentityPortNumber(String port_number) {
this.port_number = port_number;
}
//GETTING AND SETTING PHOTOCURRENT TO XML
public float getMonitorPhotoCurrent() {
return photo_current;
}
@XmlElement(name="setMonitorPhotoCurrent")
public void setMonitorPhotoCurrent(float photo_current) {
this.photo_current = photo_current;
}
//GETTING AND SETTING BIAS TO XML
//ACTUAL BIAS (READ)
public float getControlActualBias() {
return actual_bias;
}
@XmlElement (name="setControlActualBias")
public void setControlActualBias(float actual_bias) {
this.actual_bias = actual_bias;
}
//NEW BIAS (WRITE)
public float getControlNewBias(){
return new_bias;
}
@XmlElement (name="setControlNewBias")
public void setControlNewBias(float new_bias){
this.new_bias = new_bias;
}
//GETTING AND SETTING TEMP TO XML
//ACTUAL TEMP (READ)
public float getControlActualTemp() {
return actual_temp;
}
@XmlElement (name="setControlActualTemp")
public void setControlActualTemp(float actual_temp) {
this.actual_temp = actual_temp;
}
//NEW TEMP (WRITE)
public float getControlNewTemp(){
return new_temp;
}
@XmlElement(name ="setControlNewTemp")
public void setControlNewTemp(float new_temp){
this.new_temp = new_temp;
}
}
::::::::::: DSC.java ::::::::::::
public class Dsc {
public static void main(String[] args) throws InterruptedException {
//INITIALIZING SCANNER TO TAKE INPUTS
Scanner input = new Scanner(System.in);
//CALLING ALL FUNCTIONS
Identity identity = new Identity();
List<String> strings = new ArrayList<String>(2);
strings.add("1");
identity.setSlots(strings);
//CREATING SERIAL PORT OBJECT
SerialPort serialPort = new SerialPort("COM4");
//GETTING SERIALPORTS
String[] portNames = SerialPortList.getPortNames();
for(int i = 0; i < portNames.length; i++)
{
System.out.println("Port Available on this machine: " +portNames[i]);
}
identity.setIdentityPortNumber("COM4");
//STARTING TRY BLOCK TO CATCH ERRORS THROUGHOUT THE EXECUTION
try
{
//OPENING PORT AND ASSIGNING PARAMETERS FOR COMMUNICATION
System.out.println("Port opened: " + serialPort.openPort());
System.out.println("Params setted: " + serialPort.setParams(57600, 8, 1, 0));
System.out.println("--------------------------------------------------------");
//IDENTIFYING THE COMPANY NAME ; MODEL NUMBER ; SERIAL NUMBER ; PORT NUMBER
System.out.println("Passing *IDN? to identify the Device: " +serialPort.writeString("*IDN? \n"));
Thread.sleep(500);
String str = serialPort.readString();
System.out.println("The Device ID is: " +str);
String[] deviceid = str.split(",");
System.out.println("Company :" + deviceid[0]);
identity.setIdentityCompanyName(deviceid[0]);
System.out.println("Model Number :" + deviceid[1]);
identity.setIdentityModelNumber(deviceid[1]);
System.out.println("Serial Number :" + deviceid[2]);
identity.setIdentitySerialNumber(deviceid[2]);
System.out.println("--------------------------------------------------------");
//IDENTIFYING THE PHOTOCURRENT
System.out.println("Passing MEAS:IDC? to identify the Photocurrent: " +serialPort.writeString("MEAS:IDC? \n"));
Thread.sleep(500);
String str1 = serialPort.readString();
System.out.println("The Photocurrent is :" +str1);
float photoCurrent = Float.parseFloat(str1);
identity.setMonitorPhotoCurrent(photoCurrent);
System.out.println("--------------------------------------------------------");
//IDENTIFYING THE ACTUAL BIAS
System.out.println("Passing MEAS:BIAS? to identify the Actual BIAS: " +serialPort.writeString("MEAS:BIAS? \n"));
Thread.sleep(500);
String str2 = serialPort.readString();
System.out.println("The Actual BIAS is :" +str2);
float control_actualBias = Float.parseFloat(str2);
identity.setControlActualBias(control_actualBias);
System.out.println("--------------------------------------------------------");
//SETTING THE BIAS
System.out.println("Set the Bias to ?");
float setBias = input.nextFloat();
System.out.println("Setting the user input BIAS to " +setBias +": " +serialPort.writeString("INP:BIAS " +setBias +"\n" ));
Thread.sleep(500);
identity.setControlNewBias(setBias);
System.out.println("--------------------------------------------------------");
//IDENTIFYING THE ACTUAL TEMPERATURE
System.out.println("Passing MEAS:TEMP? to identify the Temperature: " +serialPort.writeString("MEAS:TEMP? \n"));
Thread.sleep(500);
String str3 = serialPort.readString();
System.out.println("The Actual TEMP is: " +str3);
float actualTemp = Float.parseFloat(str3);
identity.setControlActualTemp(actualTemp);
System.out.println("--------------------------------------------------------");
//SETTING THE TEMPERATURE
System.out.println("Set the new TEMP to ?");
float setTemp = input.nextFloat();
System.out.println("Setting the user input TEMP to " +setTemp +": " +serialPort.writeString("INP:TEMP " +setTemp +"\n" ));
Thread.sleep(500);
identity.setControlNewTemp(setTemp);
System.out.println("--------------------------------------------------------");
}
catch (SerialPortException ex)
{
System.out.println(ex);
}
//STARTING THE JAXBCONTEXT & JAXBMARSHALLER CODE TO WRITE OUTPUT IN XML FILE
try
{
File file = new File("C:\\xampp\\htdocs\\StateMachine.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Identity.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(identity, file);
jaxbMarshaller.marshal(identity, System.out);
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
:::::::: Current XML output ::::::::
<LabbuddyArray>
<setControlActualBias>5.999</setControlActualBias>
<setControlActualTemp>21.9</setControlActualTemp>
<setControlNewBias>5.0</setControlNewBias>
<setControlNewTemp>23.0</setControlNewTemp>
<setIdentityCompanyName>DSC</setIdentityCompanyName>
<setIdentityModelNumber>HLPD Lab Buddy</setIdentityModelNumber>
<setIdentityPortNumber>COM4</setIdentityPortNumber>
<setIdentitySerialNumber>50311602</setIdentitySerialNumber>
<setMonitorPhotoCurrent>0.0</setMonitorPhotoCurrent>
<slots>
<slot>1</slot>
</slots>
</LabbuddyArray>
::::::Required XML OUTPUT::::::
<LabbuddyArray>
<slot1>
<setControlActualBias></setControlActualBias>
<setControlActualTemp></setControlActualTemp>
<setControlNewBias></setControlNewBias>
<setControlNewTemp></setControlNewTemp>
<setIdentityCompanyName></setIdentityCompanyName>
<setIdentityModelNumber></setIdentityModelNumber>
<setIdentityPortNumber></setIdentityPortNumber>
<setIdentitySerialNumber></setIdentitySerialNumber>
<setMonitorPhotoCurrent></setMonitorPhotoCurrent>
</slot1>
<slot2>
<setControlActualBias></setControlActualBias>
<setControlActualTemp></setControlActualTemp>
<setControlNewBias></setControlNewBias>
<setControlNewTemp></setControlNewTemp>
<setIdentityCompanyName></setIdentityCompanyName>
<setIdentityModelNumber></setIdentityModelNumber>
<setIdentityPortNumber></setIdentityPortNumber>
<setIdentitySerialNumber></setIdentitySerialNumber>
<setMonitorPhotoCurrent></setMonitorPhotoCurrent>
</slot2>
</LabbuddyArray>
答案 0 :(得分:0)
然后你应该有这样的东西......
@XmlRootElement(name="LabbuddyArray")
public class Identity {
List<Slots> slots;
@XmlElement(name="slot")
public List<Slots> getSlots() {
return slots;
}
public void setSlots(List<Slots> slots) {
this.slots = slots;
}
}
创建一个类&#39; Slots&#39;,复制所有元素并映射到它。 并且在Dsc类中根据需要填充值。
Identity identity = new Identity();
List<Slots> slots = new ArrayList<Slots>();
Slots slot = new Slots();
slot.setControlActualBias(Float.valueOf("1.23"));
slots.add(slot);
slot = new Slots();
slot.setControlActualBias(Float.valueOf("1.24"));
slots.add(slot);
identity.setSlots(slots);
这会产生类似于下面的响应。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LabbuddyArray>
<slot>
<setControlActualBias>1.23</setControlActualBias>
<setControlActualTemp>0.0</setControlActualTemp>
<setControlNewBias>0.0</setControlNewBias>
<setControlNewTemp>0.0</setControlNewTemp>
<setMonitorPhotoCurrent>0.0</setMonitorPhotoCurrent>
</slot>
<slot>
<setControlActualBias>1.24</setControlActualBias>
<setControlActualTemp>0.0</setControlActualTemp>
<setControlNewBias>0.0</setControlNewBias>
<setControlNewTemp>0.0</setControlNewTemp>
<setMonitorPhotoCurrent>0.0</setMonitorPhotoCurrent>
</slot>
</LabbuddyArray>