我正在使用Stax parser阅读XML文件,并在java用DOM书写。 我没有获得理想的XML产出。 I 改为XML。
<config>
<Sensor1>
<name>abc</name>
<range>100</range>
</Sensor1>
<sensor2>
<name>xyz</name>
<range>100</range>
</sensor2>
</config>
I parse the above XML file using Stax parser as follows
public void readConfig(String configFile) {
boolean sensor1 = false;
boolean sensor2 = false;
try
{
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
InputStream in = new FileInputStream(configFile);
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
// Read the XML document
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
if (startElement.getName().getLocalPart() == (sensor1)) {
sensor1 = true;
Sensor1 Obj1 = new Sensor1();
}
if (startElement.getName().getLocalPart() == (sensor2)) {
sensor2 = true;
Sensor2 Obj2 = new Sensor2();
}
if (sensor1) {
if (event.asStartElement().getName().getLocalPart().equals(name)) {
event = eventReader.nextEvent();
Obj1.set_Sensor_Name(event.asCharacters().getData());
continue;
}
if (event.asStartElement().getName().getLocalPart().equals(range)) {
event = eventReader.nextEvent();
Obj1.set_Sensor_Range(event.asCharacters().getData());
continue;
}
}
if (sensor2) {
if (event.asStartElement().getName().getLocalPart().equals(name)) {
event = eventReader.nextEvent();
Obj2.set_Sensor_Name(event.asCharacters().getData());
continue;
}
if (event.asStartElement().getName().getLocalPart().equals(range)) {
event = eventReader.nextEvent();
Obj1.set_Sensor_Range(event.asCharacters().getData());
continue;
}
}
if (event.isEndElement()) {
EndElement endElement = event.asEndElement();
if (endElement.getName().getLocalPart() == (sensor1)) {
sensor1.addToArray();
}
if (endElement.getName().getLocalPart() == (sensor2)) {
sensor2.addToArray();
}
}
}
在“Sensor1”和“Sensor2”类中,我根据某些条件增加了额外信息。
class Sensor1 {
public ArrayList<Object> list = new ArrayList<Object>();
String name;
double range;
public void set_Sensor_Name(String name) {
this.name = name;
}
public void set_Sensor_Range(double range) {
this.range = range;
}
public void addToArray(){
double distance =50;
if(distance<range){
list.add("TITANIC");
list.add(123456);
}
WriteFile fileObj = new WriteFile();
fileObj.writeXMlFile(list);
}
}
这是写XML的班子。
public class WriteFile {
public void writeXmlFile(ArrayList<Object> list) {
try {
DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
DocumentBuilder build = dFact.newDocumentBuilder();
Document doc = build.newDocument();
Element root = doc.createElement("SensorTracks");
doc.appendChild(root);
Element sensorInfo = doc.createElement("SensorDetails");
root.appendChild(sensorInfo);
Element vesselInfo = doc.createElement("VesselDetails");
root.appendChild(vesselInfo);
for(int i=0; i<list.size(); i +=4 ) {
Element name = doc.createElement("SensorName");
name.appendChild(doc.createTextNode(String.valueOf(list.get(i))));
sensorInfo.appendChild(name);
Element range = doc.createElement("SensorRange");
name.appendChild(doc.createTextNode(String.valueOf(list.get(i+1))));
sensorInfo.appendChild(range);
Element mmi = doc.createElement("shipname");
mmi.appendChild(doc.createTextNode(String.valueOf(list.get(i+2))));
vesselInfo.appendChild(mmi);
Element license = doc.createElement("license");
license.appendChild(doc.createTextNode(String.valueOf(list.get(i+3))));
vesselInfo.appendChild(license);
}
// Save the document to the disk file
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
// format the XML nicely
aTransformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
aTransformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "4");
aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
try {
FileWriter fos = new FileWriter("/home/ros.xml");
StreamResult result = new StreamResult(fos);
aTransformer.transform(source, result);
} catch (IOException e) {
e.printStackTrace();
}
} catch (TransformerException ex) {
System.out.println("Error outputting document");
} catch (ParserConfigurationException ex) {
System.out.println("Error building document");
}
When I execute, I get following XML
<SensorTracks>
<sensorDetails>
<SensorName>xyz</SensorName>
<SensorRange>100</SensorRange>
</sensorDetails>
<VesselDetails>
<shipname>TITANIC</shipname>
<license>123456</license>
</vesselDetails>
最终结果
<config>
<SensorTracks>
<sensorDetails>
<SensorName>xyz</SensorName>
<SensorRange>100</SensorRange>
<SensorName>abc</SensorName>
<SensorRange>100</SensorRange>
</sensorDetails>
<VesselDetails>
<shipname>TITANIC</shipname>
<license>123456</license>
</vesselDetails>
我在守则中做了哪些错误? 感谢任何帮助。 预 收