用java将XML文件中特定的属性提取出来并保存成XML文件 20
原始文件类似<timesteptime="0.00"><vehicleid="0"x="475.24"y="609.24"angle="84.69"type="DEFAU...
原始文件类似
<timestep time="0.00">
<vehicle id="0" x="475.24" y="609.24" angle="84.69" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="-116866313#2_0" slope="0.00"/>
</timestep>
<timestep time="1.00">
<vehicle id="0" x="476.68" y="609.10" angle="84.69" type="DEFAULT_VEHTYPE" speed="1.44" pos="6.54" lane="-116866313#2_0" slope="0.00"/>
<vehicle id="1" x="451.90" y="677.22" angle="165.77" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="-116866242#1_0" slope="0.00"/>
</timestep>
<timestep time="2.00">
<vehicle id="0" x="479.78" y="608.82" angle="84.69" type="DEFAULT_VEHTYPE" speed="3.12" pos="9.66" lane="-116866313#2_0" slope="0.00"/>
<vehicle id="1" x="452.55" y="679.74" angle="165.72" type="DEFAULT_VEHTYPE" speed="2.60" pos="7.70" lane="-116866242#1_0" slope="0.00"/>
<vehicle id="2" x="408.90" y="637.98" angle="179.08" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="-116866410#2_0" slope="0.00"/>
</timestep>
生成的文件
<root>
<Position>
<time>0.00</time>
<ID>t0</ID>
<x>475.24</x>
<y>609.24</y>
</Position>
<Position>
<time>1.00</time>
<ID>t0</ID>
<x>476.68</x>
<y>609.10</y>
</Position>
</root> 展开
<timestep time="0.00">
<vehicle id="0" x="475.24" y="609.24" angle="84.69" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="-116866313#2_0" slope="0.00"/>
</timestep>
<timestep time="1.00">
<vehicle id="0" x="476.68" y="609.10" angle="84.69" type="DEFAULT_VEHTYPE" speed="1.44" pos="6.54" lane="-116866313#2_0" slope="0.00"/>
<vehicle id="1" x="451.90" y="677.22" angle="165.77" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="-116866242#1_0" slope="0.00"/>
</timestep>
<timestep time="2.00">
<vehicle id="0" x="479.78" y="608.82" angle="84.69" type="DEFAULT_VEHTYPE" speed="3.12" pos="9.66" lane="-116866313#2_0" slope="0.00"/>
<vehicle id="1" x="452.55" y="679.74" angle="165.72" type="DEFAULT_VEHTYPE" speed="2.60" pos="7.70" lane="-116866242#1_0" slope="0.00"/>
<vehicle id="2" x="408.90" y="637.98" angle="179.08" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="-116866410#2_0" slope="0.00"/>
</timestep>
生成的文件
<root>
<Position>
<time>0.00</time>
<ID>t0</ID>
<x>475.24</x>
<y>609.24</y>
</Position>
<Position>
<time>1.00</time>
<ID>t0</ID>
<x>476.68</x>
<y>609.10</y>
</Position>
</root> 展开
1个回答
展开全部
你好,这是解析xml的代码:
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class ReadAndPrintXMLFile{
public static void main (String argv []){
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("book.xml"));
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
NodeList listOfPersons = doc.getElementsByTagName("person");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
for(int s=0; s<listOfPersons.getLength() ; s++){
Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
Element firstPersonElement = (Element)firstPersonNode;
//-------
NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
Element firstNameElement = (Element)firstNameList.item(0);
NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("First Name : " +
((Node)textFNList.item(0)).getNodeValue().trim());
//-------
NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
Element lastNameElement = (Element)lastNameList.item(0);
NodeList textLNList = lastNameElement.getChildNodes();
System.out.println("Last Name : " +
((Node)textLNList.item(0)).getNodeValue().trim());
//----
NodeList ageList = firstPersonElement.getElementsByTagName("age");
Element ageElement = (Element)ageList.item(0);
NodeList textAgeList = ageElement.getChildNodes();
System.out.println("Age : " +
((Node)textAgeList.item(0)).getNodeValue().trim());
//------
}//end of if clause
}//end of for loop with s var
}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}catch (Throwable t) {
t.printStackTrace ();
}
//System.exit (0);
}//end of main
}
<book>
<person>
<first>Kiran</first>
<last>Pai</last>
<age>22</age>
</person>
<person>
<first>Bill</first>
<last>Gates</last>
<age>46</age>
</person>
<person>
<first>Steve</first>
<last>Jobs</last>
<age>40</age>
</person>
</book>
Program Output
Root element of the doc is book
Total no of people : 3
First Name : Kiran
Last Name : Pai
Age : 22
First Name : Bill
Last Name : Gates
Age : 46
First Name : Steve
Last Name : Jobs
Age : 40
你可以参考,至于生成xml就简单了
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class ReadAndPrintXMLFile{
public static void main (String argv []){
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("book.xml"));
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
NodeList listOfPersons = doc.getElementsByTagName("person");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
for(int s=0; s<listOfPersons.getLength() ; s++){
Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
Element firstPersonElement = (Element)firstPersonNode;
//-------
NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
Element firstNameElement = (Element)firstNameList.item(0);
NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("First Name : " +
((Node)textFNList.item(0)).getNodeValue().trim());
//-------
NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
Element lastNameElement = (Element)lastNameList.item(0);
NodeList textLNList = lastNameElement.getChildNodes();
System.out.println("Last Name : " +
((Node)textLNList.item(0)).getNodeValue().trim());
//----
NodeList ageList = firstPersonElement.getElementsByTagName("age");
Element ageElement = (Element)ageList.item(0);
NodeList textAgeList = ageElement.getChildNodes();
System.out.println("Age : " +
((Node)textAgeList.item(0)).getNodeValue().trim());
//------
}//end of if clause
}//end of for loop with s var
}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}catch (Throwable t) {
t.printStackTrace ();
}
//System.exit (0);
}//end of main
}
<book>
<person>
<first>Kiran</first>
<last>Pai</last>
<age>22</age>
</person>
<person>
<first>Bill</first>
<last>Gates</last>
<age>46</age>
</person>
<person>
<first>Steve</first>
<last>Jobs</last>
<age>40</age>
</person>
</book>
Program Output
Root element of the doc is book
Total no of people : 3
First Name : Kiran
Last Name : Pai
Age : 22
First Name : Bill
Last Name : Gates
Age : 46
First Name : Steve
Last Name : Jobs
Age : 40
你可以参考,至于生成xml就简单了
追答
你好,麻烦采纳一下哦
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询