java是如何解析xml文档的,如何实现java编辑xml文档?java如何读取xml文档?给个例子说明一下最好。谢谢!

解决方案 »

  1.   

    用框架dom4j处理xml,如果你想要知道底层如何处理的,其实很简单,就是字符串的处理过滤筛选等等。相当与编译过程。
      

  2.   

    具体的例子看:http://dom4j.sourceforge.net/dom4j-1.6.1/guide.html
    One of the first things you'll probably want to do is to parse an XML document of some kind. This is easy to do in dom4j. The following code demonstrates how to this.import java.net.URL;import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.io.SAXReader;public class Foo {    public Document parse(URL url) throws DocumentException {
            SAXReader reader = new SAXReader();
            Document document = reader.read(url);
            return document;
        }
    }
    A document can be navigated using a variety of methods that return standard Java Iterators. For example    public void bar(Document document) throws DocumentException {        Element root = document.getRootElement();        // iterate through child elements of root
            for ( Iterator i = root.elementIterator(); i.hasNext(); ) {
                Element element = (Element) i.next();
                // do something
            }        // iterate through child elements of root with element name "foo"
            for ( Iterator i = root.elementIterator( "foo" ); i.hasNext(); ) {
                Element foo = (Element) i.next();
                // do something
            }        // iterate through attributes of root 
            for ( Iterator i = root.attributeIterator(); i.hasNext(); ) {
                Attribute attribute = (Attribute) i.next();
                // do something
            }
         }
      

  3.   

    java处理xml文档有两种主要模式。DOM和SAX。DOM是把xml作为树状结构来处理,而SAX序列地扫描xml文档,并激发各种事件,应用程序实现处理这些事件的代码。后者更适合与非常大的xml文档,因为被扫描的文档不需要全部在内存里面。详细情况可以搜索java dom或java sax。
      

  4.   

    public class GetConfig {

    private String userName = "";
    private String passWord = "";
    private Document doc = null;

    public GetConfig(){}

    public Document getDoc(){
    SAXReader sr = new SAXReader();
    try {
    doc = sr.read(new File("user-config.xml"));
    //doc = sr.read(this.getClass().getClassLoader().getResource(("user-config.xml")));
    } catch (DocumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    System.out.println("读取失败");
    }
    return doc;
    }

    public void readConfig(){
    this.getDoc();
    List<Element> nameList = doc.selectNodes("/users/user/name");
    List<Element> passWordList = doc.selectNodes("/users/user/password");
    userName = nameList.get(0).getText();
    passWord = passWordList.get(0).getText();
    System.out.println(userName + passWord);
    }

    public static void main(String[] args){
    GetConfig gc = new GetConfig();
    gc.readConfig();
    }

    public String getUserName(){
    return this.userName;
    }

    public String getPassWord(){
    return this.passWord;
    }
    }采用xpath方式读取,建议LZ先熟悉一下dom结构。
      

  5.   

     最简单和最频繁使用的就是dom4j了  
      

  6.   

    哈哈。
    你也可以用开源的XStream,
    Java对象转换为xml文件,xml文件再转换为Java对象。
      

  7.   

    我就是想用xml来保存通讯录然后用java来管理通讯录,想知道java如何实现读取和写入xml文档的