java 读取xml文件部分内容 并创建新的xml文件
假如xml文件a.xml:<book><book1><name>English</name><price>50</price></book1></book>我想在这个xm...
假如xml文件a.xml:
<book>
<book1><name>English</name><price>50</price></book1>
</book>
我想在这个xml文件里面取出book1的相关内容并存在新的xml文件中:b.xml
<book1><name>English</name><price>50</price></book1> 展开
<book>
<book1><name>English</name><price>50</price></book1>
</book>
我想在这个xml文件里面取出book1的相关内容并存在新的xml文件中:b.xml
<book1><name>English</name><price>50</price></book1> 展开
2个回答
展开全部
下面的代码,你自己看看吧。希望能帮了你,能读取a.xml中的book1的内容。至于写入b.xml中可以String写,也可以xml写。
import java.io.File;
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();
//read a.xml
Document doc = docBuilder.parse (new File("D:\\a.xml"));
// normalize text redivsentation
doc.getDocumentElement ().normalize ();
//System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
//get <book1> the node you want!!
NodeList listOfPersons = doc.getElementsByTagName("book1");
for(int s=0; s<listOfPersons.getLength() ; s++){
Node firstPersonNode = listOfPersons.item(s);
//get all propertities of the node you want.
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
Element firstPersonElement = (Element)firstPersonNode;
//-------
NodeList firstNameList = firstPersonElement.getElementsByTagName("name");
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("price");
Element lastNameElement = (Element)lastNameList.item(0);
NodeList textLNList = lastNameElement.getChildNodes();
//System.out.println("Last Name : " +
// ((Node)textLNList.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
}
import java.io.File;
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();
//read a.xml
Document doc = docBuilder.parse (new File("D:\\a.xml"));
// normalize text redivsentation
doc.getDocumentElement ().normalize ();
//System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
//get <book1> the node you want!!
NodeList listOfPersons = doc.getElementsByTagName("book1");
for(int s=0; s<listOfPersons.getLength() ; s++){
Node firstPersonNode = listOfPersons.item(s);
//get all propertities of the node you want.
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
Element firstPersonElement = (Element)firstPersonNode;
//-------
NodeList firstNameList = firstPersonElement.getElementsByTagName("name");
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("price");
Element lastNameElement = (Element)lastNameList.item(0);
NodeList textLNList = lastNameElement.getChildNodes();
//System.out.println("Last Name : " +
// ((Node)textLNList.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
}
展开全部
用java自带的就可以,有问题可以问我
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
//构造
public XMLUtil(String name) throws ParserConfigurationException {
filename = name;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
document = builder.newDocument();
}
/**
* 保存到文件
*/
public void toSave() {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
StreamResult result = new StreamResult(pw);
transformer.transform(source, result);
} catch (TransformerException mye) {
mye.printStackTrace();
} catch (IOException exp) {
exp.printStackTrace();
}
}
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
vaela
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
//构造
public XMLUtil(String name) throws ParserConfigurationException {
filename = name;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
document = builder.newDocument();
}
/**
* 保存到文件
*/
public void toSave() {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
StreamResult result = new StreamResult(pw);
transformer.transform(source, result);
} catch (TransformerException mye) {
mye.printStackTrace();
} catch (IOException exp) {
exp.printStackTrace();
}
}
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
vaela
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询