import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class XMLTest {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
try {
String xml="<gengohs><gengoh>昭和<from>1926</from><max>64</max></gengoh></gengohs>";
//以下为解析XML BEGIN=====
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
StringReader rd = new StringReader(xml);
InputSource is = new InputSource(rd);
Document doc=dombuilder.parse(is);
Element root=doc.getDocumentElement();
NodeList gengohs = root.getChildNodes();
if (gengohs != null) {
for (int i = 0; i < gengohs.getLength(); i++) {
Node gengoh = gengohs.item(i);
if (gengoh.getNodeType() == Node.ELEMENT_NODE) {
String Lable=gengoh.getFirstChild().getNodeValue();
System.out.print(Lable);
}
}
}
//解析XML END============
} catch (Exception e){
e.printStackTrace();
}
}
}
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class XMLTest {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
try {
String xml="<gengohs><gengoh>昭和<from>1926</from><max>64</max></gengoh></gengohs>";
//以下为解析XML BEGIN=====
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
StringReader rd = new StringReader(xml);
InputSource is = new InputSource(rd);
Document doc=dombuilder.parse(is);
Element root=doc.getDocumentElement();
NodeList gengohs = root.getChildNodes();
if (gengohs != null) {
for (int i = 0; i < gengohs.getLength(); i++) {
Node gengoh = gengohs.item(i);
if (gengoh.getNodeType() == Node.ELEMENT_NODE) {
String Lable=gengoh.getFirstChild().getNodeValue();
System.out.print(Lable);
}
}
}
//解析XML END============
} catch (Exception e){
e.printStackTrace();
}
}
}
解决方案 »
- spring与webwork整合需要的jar包?
- 求助:哪位大哥哥有 commons-fileupload 组件的中文文档啊,小弟实在是看不懂英文文档,- -!
- 求救!!!Hibernate 复合主键作外键且一对多关联问题!
- 关于删除的问题.
- 一个简单的气死人,急死人的JSTL问题!
- Spring ModelAndView jstl 取值问题
- 数据库连接的问题
- 求救:关于struts的一个问题,急急!!!!!
- 开源也不怎么样---搞Hibernate和EJB的一点感想
- DOM中判断XML文件中的一个接点是否存在的方法是什么?
- 用jbuilder开发webservice碰到的问题,求高人解答
- 冰天雪地跪求Jvision1.42 sn序列号啊.
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXWriter;
import org.dom4j.io.SAXModifier;
import org.dom4j.io.XMLWriter;
import org.dom4j.XPath;public class XMLOperation {
private File f;
private Document doc;
private Element root;
private SAXReader reader;
private XMLWriter writer;
private SAXModifier modifier;
public XMLOperation() {
}
public XMLOperation(File f) throws DocumentException {
this.f = f;
reader = new SAXReader();
}
public Node getSingleElementByXPath(String condition) {
Node node = null;
try {
doc = reader.read(f);
node = doc.selectSingleNode(condition);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return node;
}
public static void main(String[] args) throws UnsupportedEncodingException {
XMLOperation xo;
try {
xo = new XMLOperation(new File("e:\\createXml.xml"));
Node node = xo.getSingleElementByXPath("/gengoh");
System.out.println(node.getText());
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("end");
}
}
thanks
hima