我必须使用JAVA生成的示例XML 感 168 192.168.140.150 002EC0FEFF83EA97
使用过的代码我到目前为止已写过
import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class SampleCode {
public static final String xmlFilePath = "C:\\Users\\harsh.sharma\\Desktop\\Sense Performance\\Generated\\xmlfile.xml";
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
try {
String cid_Value="168";
String ip_Value="192.168.140.150";
String id_Value="002EC0FEFF83EA97";
String sensorsid_Value="002EC0FEFF8FFF27";
String desc_Value=" ";
String batt_Value="6.60";
String sig_Value="-55";
String scount_Value="0";
String rdate_Value="15/05/2015 21:47:04";
DateFormat dateFormat = new SimpleDateFormat("dd/MM/YYYY HH:mm:ss");
//get current date time with Date()
@SuppressWarnings("deprecation")
Calendar cal = Calendar.getInstance();
String mdate_Value=dateFormat.format(cal.getTime());
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
//root element
Element root = document.createElement("msgs");
document.appendChild(root);
//msg element
Element msg = document.createElement("msg");
root.appendChild(msg);
//type element
Element type = document.createElement("type");
msg.appendChild(type);
type.setTextContent("Sense");
//cid element
Element cid = document.createElement("cid");
msg.appendChild(cid);
cid.setTextContent(cid_Value);
//ip element
Element ip = document.createElement("ip");
msg.appendChild(ip);
ip.setTextContent(ip_Value);
Element errs = document.createElement("errs");
msg.appendChild(errs);
//id element
Element id = document.createElement("id");
msg.appendChild(id);
id.setTextContent(id_Value);
//sensors element
Element sensors = document.createElement("sensors");
msg.appendChild(sensors);
//Sensor element
Element sensor = document.createElement("sensor");
//sensors.appendChild(sensor);
sensor.setAttribute("sensorsid",sensorsid_Value);
sensor.setAttribute("desc",desc_Value);
sensor.setAttribute("batt",batt_Value);
sensor.setAttribute("sig", sig_Value);
sensor.setAttribute("scount",scount_Value);
sensor.setAttribute("rdate",rdate_Value);
sensor.setAttribute("mdate",mdate_Value);
sensors.appendChild(sensor);
// create the xml file
//transform the DOM Object to an XML File
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(new File(xmlFilePath));
// If you use
// StreamResult result = new StreamResult(System.out);
// the output will be pushed to the standard output ...
// You can use that for debugging
transformer.transform(domSource, streamResult);
System.out.println("Done creating XML File");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
}
我想它应该在传感器元素下给我多行,我想写一个循环,我可以为传感器元素生成多个数据