java 读取xml文件部分内容 并创建新的xml文件

2024-11-24 10:13:08
推荐回答(2个)
回答1:

下面的代码,你自己看看吧。希望能帮了你,能读取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 the node you want!!
NodeList listOfPersons = doc.getElementsByTagName("book1");

for(int s=0; 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

}

回答2:

用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