1
File file = new File("E:\\file.xml");

JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);

Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 

jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

jaxbMarshaller.marshal(customer, file);     

jaxbMarshaller.marshal(customer, System.out);

我可以创建文件,但下次想要追加到 xml 中而不是创建。

4

4 回答 4

2

它不会以这种方式工作。您必须创建客户类

@XmlRootElement
class Customers {
   @XmlElement(name="customer")
   List<Customer> customers;
}

每次您想要添加客户时,您需要解组customers.xml,将新客户添加到客户列表并再次将客户编组到customers.xml。

于 2013-01-10T10:28:07.373 回答
0

此 java 代码用于将新节点附加到 xml 文件......它基于 DOM

import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileOutputStream;
public class writexml1 {

public static void main (String args[]) 
{

File docFile = new File("..\\jquery\\WebContent\\demo\\testing.xml");

Document doc = null;
try 
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docFile);
} 
catch (java.io.IOException e) 
{
System.out.println("Can't find the file");
} 
catch (Exception e) 
{
System.out.print("Problem parsing the file.");
}

Element root = doc.getDocumentElement();

System.out.println("The root element is " + root.getNodeName() + ".\n");

NodeList children = root.getChildNodes();
System.out.print("There are "+children.getLength()+" child elements.\n");
System.out.print("They are: \n");

//Print the file 
for (Node child = root.getFirstChild();child != null;child = child.getNextSibling())
{
if (child.getNodeType() == child.TEXT_NODE)
{
System.out.println("Text: "+child.getNodeValue());
} 
else if (child.getNodeType() == child.ELEMENT_NODE) 
{
System.out.println(child.getNodeName()+" = "+child.getFirstChild().getNodeValue());
}
}


//NodeList deleteElement = root.getElementsByTagName("staff");

//Node deleteNode= deleteElement.item(0);

//root.removeChild(deleteNode); 
Element staffElement = doc.createElement("staff");

Node updateText = doc.createTextNode("");
staffElement.appendChild(updateText);
//
Element firstName = doc.createElement("firstname");
String str_firstName="added firstname";
Node firstNameNode = doc.createTextNode(str_firstName);
firstName.appendChild(firstNameNode);

staffElement.appendChild(firstName);

//

Element lastName = doc.createElement("lastname");
String str_lastName="added lastname";
Node lastNameNode = doc.createTextNode(str_lastName);
lastName.appendChild(lastNameNode);

staffElement.appendChild(lastName);


//
Element nickName = doc.createElement("nickname");
String str_nickName="added nickname";
Node nickNameNode = doc.createTextNode(str_nickName);
nickName.appendChild(nickNameNode);

staffElement.appendChild(nickName);


//
Element salary = doc.createElement("salary");
String str_salary="$10,000";
Node salaryNode = doc.createTextNode(str_salary);
salary.appendChild(salaryNode);

staffElement.appendChild(salary);


//
root.appendChild(staffElement);

//Node StaffNode=(Node)updateElement;





try{
String outputURL = "..\\jquery\\WebContent\\demo\\testing.xml";

DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new FileOutputStream(outputURL));

TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();

transformer.transform(source, result);

} catch (Exception e) {
e.printStackTrace();
}

}   
   }

或者

您应该查看 JAXB API。如果我理解正确,你的 xml 看起来像这样:

<B>
    <C>11</C>
    <D>21</D>
    <E>31</E>
</B>

所以代码是:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class B {
    @XmlElement public String C; // sloppy, probably should be type Integer or something
    @XmlElement public String D;
    @XmlElement public String E;
}

// then, somewhere else in your code you want to serialize...
B b = new B();
b.C = "11";
b.D = "21";
b.E = "31";

JAXBContext c = JAXBContext.newInstance(B.class);

// where w is a Writer instance
c.createMarshaller().marshal(b, w);
于 2013-01-10T10:19:43.463 回答
0

有一个版本需要. 看看这个线程,创建一个 FileWriter 并使用它。marshallWriter

于 2013-01-10T10:22:37.213 回答
-1

我认为代码new FileWriter(file, true)将帮助您将数据附加到现有文件中。

于 2013-01-10T10:21:12.667 回答