用castor,jdom都可以,到网上查一下这个的帮助吧,建议用castor
解决方案 »
- 疑问解答,另希望找到一好心的朋友能够指导我!
- 请各位神爷帮帮忙,在往数据库导入excel表的数据是报错!怎么回事,我在另外一台电脑试都没事的
- 请各位大神推荐几个好点得空间,域名
- 用java操作硬件
- MySQL 树 递归
- Action里面不能绑定值
- 请问关于由数据显示图片的问题,以下是我的代码,其中img是图片数据
- 100急求SQL Server的datetime映射到Hibernate的什么类型?
- 参照分布式和企业系统结构、及应用开发的重点明显地转向基于构件的应用开发,讨论其他应用开发模式的相对的优缺点
- 近期想学j2ee,请大家给点这方面书的推荐!
- 这个struts-config.xml文件配置有什么问题?
- 急!!我在tomcat下面可以,但是weblogic下面就不行,怎么回事?
Person person = new Person("Ryan 'Mad Dog' Madden");
person.setDateOfBirth(new Date(1955, 8, 15));
try{
// Create a File to marshal to
Writer writer = new FileWriter("test.xml");
// Marshal the person object
Marshaller mar = new Marshaller(writer);
mar.marshal(person);
}catch(Exception e){}
}
}上面这段代码意思很简单
但是不知道为什么会在Marshaller mar = new Marshaller(writer);报错!
ava.lang.NoClassDefFoundError: org/apache/xml/serialize/XMLSerializer
at org.exolab.castor.xml.Marshaller.initialize(Marshaller.java:397)
at org.exolab.castor.xml.Marshaller.<init>(Marshaller.java:358)
at Xml.main(Xml.java:32)
Exception in thread "main" 是不是没有org/apache/xml/serialize/XMLSerializer着个包在哪里有呀!难道有java.io.Serializer不一样,是由于解析XML序列化不一样的吗
并且要用document对象获得xml文件信息,在对dom对象操作
获得结点值及属性值,根据你的需求应该不难
查查帮助,有例子
以上内容,仅供参考!!!
person.setDateOfBirth(new Date());
Writer writer = new FileWriter("test.xml");
Marshaller mar = new Marshaller(writer);
mar.marshal(person);
System.out.println("Writer OK!!!!");
Reader reader = new FileReader("test.xml");
Person person2 = (Person)Unmarshaller.unmarshal(Person.class, reader);
已经可以实现了,可以写到test.xml也可以读
但是还有问题,就是对条数据怎么写进去,读出来应该是个Collection但是我怎么在api里找不到插入和读多条数据的方法!
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.lang.System;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.util.Vector;import org.w3c.dom.*;
import org.w3c.dom.Document.*;import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xerces.dom.DocumentImpl;public class XmlApi{ private DocumentBuilderFactory factory ;
private DocumentBuilder bulider;
private Document doc;
/********构造函数********/
public XmlApi(String PATH) throws Exception
{
factory = DocumentBuilderFactory.newInstance();
bulider = factory.newDocumentBuilder();
doc = bulider.parse(PATH);
}
/********取值*******/
public String GetValue(String Node) throws Exception
{
NodeList nl = doc.getElementsByTagName(Node);
String Value = nl.item(0).getFirstChild().getNodeValue().trim();
return Value;
}
/*********根据值查找节点*******/
/***** index >= 0 为找到该节点 ******/
/***** index = -1 找无节点位此值 ******/
/***** index >= 0 为无该节点 ******/
public int Found(String Node , String value) throws Exception
{
int index = -1;
NodeList nl = doc.getElementsByTagName(Node);
int len = nl.getLength();
if(len <= 0)
return -2;
for(int i = 0; i < len; i++)
{
String NodeValue = nl.item(0).getFirstChild().getNodeValue().trim();
if(value.equals(NodeValue))
{
index = i;
break;
}
}
return index;
}
/********修改***********/
public void Mod(String Node, String Value) throws Exception
{
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element) nl.item(0);
Ele.getFirstChild().setNodeValue(Value);
}
public void Mod(String Node, String Value, int index) throws Exception
{
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element) nl.item(index);
Ele.getFirstChild().setNodeValue(Value);
}
/*********修改子节点内容**********/
public void ModChild(String Node, String ChildNode, String Value)
{
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element)nl.item(0);
Ele.getElementsByTagName(ChildNode).item(0).getFirstChild().setNodeValue(Value);
}
public void ModChild(String Node, String ChildNode, String Value, int index)
{
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element)nl.item(index);
Ele.getElementsByTagName(ChildNode).item(0).getFirstChild().setNodeValue(Value);
}
/*****设置属性****/
public void SetAtt(String Node, String Attribute, String Value)
{
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element) nl.item(0);
Ele.setAttribute(Attribute, Value);
}
public void SetAtt(String Node, String Attribute, String Value, int index)
{
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element) nl.item(index);
Ele.setAttribute(Attribute, Value);
}
/********添加文本节点***********/
public void AddTxt(String Node, String Value) throws Exception
{
Text txt = doc.createTextNode(Value);
doc.getElementsByTagName(Node).item(0).appendChild(txt);
} public void AddTxt(String Node, String Value, int index) throws Exception
{
Text txt = doc.createTextNode(Value);
doc.getElementsByTagName(Node).item(index).appendChild(txt);
}
/*********子节点添加文本节点**********/
public void AddChildTxt(String Node, String ChildNode, String Value)
{
Text txt = doc.createTextNode(Value);
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element)nl.item(0);
Ele.getElementsByTagName(ChildNode).item(0).appendChild(txt);
}
public void AddChildTxt(String Node, String ChildNode, String Value, int index)
{
Text txt = doc.createTextNode(Value);
NodeList nl = doc.getElementsByTagName(Node);
Element Ele = (Element)nl.item(index);
Ele.getElementsByTagName(ChildNode).item(0).appendChild(txt);
}
/*********添加节点和值*******************/
public void Add(String ParentNode, String ChildNode, String Value) throws Exception
{
Element Child = doc.createElement(ChildNode);
Text txt = doc.createTextNode(Value);
doc.getElementsByTagName(ParentNode).item(0).appendChild(Child).appendChild(txt);
}
public void Add(String ParentNode, String ChildNode, String Value , int index) throws Exception
{
Element Child = doc.createElement(ChildNode);
Text txt = doc.createTextNode(Value);
doc.getElementsByTagName(ParentNode).item(index).appendChild(Child).appendChild(txt);
}
/************删除节点***********/
public void Del(String DelNode) throws Exception
{
Node ChildNode = doc.getElementsByTagName(DelNode).item(0);
Node ParentNode = ChildNode.getParentNode();
ParentNode.removeChild(ChildNode);
}
public void Del(String DelNode, int index) throws Exception
{
Node ChildNode = doc.getElementsByTagName(DelNode).item(index);
Node ParentNode = ChildNode.getParentNode();
ParentNode.removeChild(ChildNode);
}
/*********把Vector的内容添加导xml中*****************/
public void VectorToXml(Vector Name, Vector Data) throws Exception
{
int k = 0;
int cells = Name.size() - 2;
int rows = Data.size()/cells;
String rootName = (String)Name.elementAt(0);
String nodeName = (String)Name.elementAt(0);
Node root = doc.createElement(rootName);
doc.getDocumentElement().appendChild(root);
for(int i = 0; i < rows; i++)
{
Node node = doc.createElement(nodeName);
root.appendChild(node);
for(int j = 0; j < cells; j++)
{
String memberName = (String)Name.elementAt(j+2);
Node member = doc.createElement(memberName);
String Value = (String)Data.elementAt(k);
Text txt = doc.createTextNode(Value);
node.appendChild(member).appendChild(txt);
k++;
}
}
}
/***********写入文件**************************/ public void Write(String PATH) throws Exception
{
FileWriter writer = new FileWriter(PATH);
OutputFormat outputFormat = new OutputFormat(doc, "GB2312", true);
XMLSerializer serializer = new XMLSerializer(writer, outputFormat);
serializer.serialize(doc);
writer.close();
}
/*************合并XML文件***********/
public void unite(String TPATH) throws Exception
{
try{
Document Tdoc = bulider.parse(TPATH);
Node tmp = doc.importNode(Tdoc.getDocumentElement(),true);
doc.getDocumentElement().appendChild(tmp);
NodeList nl = doc.getElementsByTagName("CName");
String NodeValue = nl.item(0).getFirstChild().getNodeValue().trim();
}
catch(Exception e)
{
}
}
}