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>
展开
 我来答
纸迷金醉aaly
2010-10-09 · 超过29用户采纳过TA的回答
知道答主
回答量:109
采纳率:0%
帮助的人:77.3万
展开全部
下面的代码,你自己看看吧。希望能帮了你,能读取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

}
陈学阳
2010-10-10 · TA获得超过2.8万个赞
知道大有可为答主
回答量:2.1万
采纳率:14%
帮助的人:5617万
展开全部
用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
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式