XML操作(dom,sax,jdom,dom4j)

1>    Dom

XML(Extensible Markup Language 可扩展标记语言)

<1>DOMDocument Object Model(文档对象模型)

DOM的特性:定义一组 Java 接口,基于对象,与语言和平台无关

       XML 文档表示为树,在内存中解析和存储 XML 文档,允许随机访问文档的不同部

 

       使用 DOM 解析 XML 文档:

       获得一个新 DocumentBuilderFactory 实例

       使用 DocumentBuilderFactory 构建 DocumentBuilder

       使用 DocumentBuilder parse( ) 方法解析文件

       将已解析的文档存储在 Document 对象中

       使用 getElementsByTagName( ) 方法获得元素

                    

       DOM的缺点:须将整个文档存储在内存中

代码如下:try {

           //获取文件路径

           //String filePath = request.getSession().getServletContext().getRealPath("/WEB-INF/xmldata/staticdata.xml");

           //构造File对象

           File f = new File("staticdata.xml");

          

           //获取文档解析器工厂对象

           DocumentBuilderFactory factory = DocumentBuilderFactory

                  .newInstance();

           //通过解析器工厂生成文档解析器对象

           DocumentBuilder builder = factory.newDocumentBuilder();

           //解析文件并返回Document对象

           Document doc = builder.parse(f);

          

           //获取XML文件中所有的"attr"节点列表

           NodeList nl = doc.getElementsByTagName("attr");

          

          

           Node attrcode = nl.item(0).getAttributes().getNamedItem("code");

           Node attrname = nl.item(0).getAttributes().getNamedItem("name");

           System.out.println(attrcode.getNodeValue()+","+attrname.getNodeValue());

           System.out.println();

          

           NodeList items = doc.getElementsByTagName("item");

           for(int i=0;i<items.getLength();i++){

              System.out.print("code:");

              String code = items.item(i).getAttributes().getNamedItem("code").getNodeValue();

              String name = items.item(i).getAttributes().getNamedItem("name").getNodeValue();

              System.out.print(code);

              System.out.println(",name:"+name);

           }

          

       } catch (Exception e) {

           e.printStackTrace();

    }

2>    Sax(由于实际项目中很少使用,只作了解)

使用 SAX 解析 XML 文档的步骤:

       创建 SAXParserFactory 的实例

       创建 SAXParser 的实例

       创建 SAXParserHandler

       使用 parse() 方法解析 XML 文档

 

       SAX 的缺点:

       不能对文档进行随机访问

       只读

       只遍历文档一次

实例代码如下:

Stack tags=new Stack();

      String name;

      String address;

      String tel;

      String fax;

      String email;

 

      public void endDocument() throws SAXException {

        System.out.println("------Parse End--------");

       }

 

      public void startDocument() throws SAXException {

        System.out.println("------Parse Begin--------");

       }

 

      public void startElement(String p0, String p1, String p2, Attributes p3) throws SAXException {

        tags.push(p2);

       }

 

      public void endElement(String p0, String p1, String p2) throws SAXException {

        tags.pop();

        if (p2.equals("PERSON")) printout();

       }

 

      public void characters(char[] p0, int p1, int p2) throws SAXException {

        String tag=(String) tags.peek();

 

        if (tag.equals("NAME")) name=new String(p0,p1,p2);

        else if (tag.equals("ADDRESS")) address=new String(p0,p1,p2);

        else if (tag.equals("TEL")) tel=new String(p0,p1,p2);

        else if (tag.equals("FAX")) fax=new String(p0,p1,p2);

        else if (tag.equals("EMAIL")) email=new String(p0,p1,p2);

       }

 

      private void printout(){

        System.out.print("Name: ");

        System.out.println(name);

        System.out.print("Address: ");

        System.out.println(address);

        System.out.print("Tel: ");

        System.out.println(tel);

        System.out.print("Fax: ");

        System.out.println(fax);

        System.out.print("Email: ");

        System.out.println(email);

        System.out.println();

        }

 

      static public void main(String[] args) {

        String filename = "candidate.xml";

        SAXParserFactory spf = SAXParserFactory.newInstance();

        SAXParser saxParser=null;

 

        try {

            saxParser = spf.newSAXParser();

        } catch (Exception e) {

            System.out.println(e);

            System.exit(1);

        }

        try {

            saxParser.parse(new File(filename),new SaxTest());

        } catch (SAXException se) {

            System.out.println(se.getMessage());

            System.exit(1);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值