NanoXML组件解析xml简单例子

NanoXML组件解析xml简单例子

 

 

 package com.aspboy.base.action.util;

import java.util.Iterator;
import java.util.Vector;

import net.n3.nanoxml.IXMLElement;
import net.n3.nanoxml.IXMLParser;
import net.n3.nanoxml.IXMLReader;
import net.n3.nanoxml.StdXMLReader;
import net.n3.nanoxml.XMLParserFactory;

public class NanoXmlTest {

 public static void main(String[] args) {

  // 解析
  //String body = "<m a='1' b='2'><a id='vvv'>111111</a><b>222222</b></m>";
  //String body = "<m a='1' b='2'><a id='1'>111111</a><a id='2'>222222</a><b id='32222'>333333</b></m>";
  
  String body = "<m a='1' b='2'><a id='1'>111111</a><c id='2'>222222</c><b id='32222'>333333</b></m>";
  
  IXMLParser xmlParser = null;
  IXMLReader xmlReader = null;
  try {
   long a = System.currentTimeMillis();
   xmlParser = XMLParserFactory.createDefaultXMLParser();
   System.out.println(System.currentTimeMillis() - a + "ms");// 1
   a = System.currentTimeMillis();
   xmlReader = StdXMLReader.stringReader(body);
   xmlParser.setReader(xmlReader);
   IXMLElement header = (IXMLElement) xmlParser.parse();
   //IXMLElement info = header.getFirstChildNamed("m");
   //System.out.println(info.getAttribute("a", ""));
   System.out.println(header.getAttribute("a", ""));
   System.out.println(header.getAttribute("b", ""));
   System.out.println(header.getContent());
   
   
   //System.out.println(header.getFirstChildNamed("a").getContent());
   System.out.println(header.getFirstChildNamed("b").getContent());
   
   IXMLElement node1 = header.getFirstChildNamed("b");
   System.out.println(node1.getAttribute("id", ""));
   
   //System.out.println(header.getChildrenCount());
   
   Vector v=header.getChildrenNamed("a");
   for (int i=0;i<v.size();i++)
   {
    IXMLElement ai=(IXMLElement)v.elementAt(i);
    long id = ai.getAttribute("id", 0);
    System.out.println("id="+id+"=="+ai.getContent());
    
   }
  
   //IXMLElement node = header.getFirstChildNamed("a");
   Iterator it = header.getChildren().iterator();
   while (it.hasNext())
   {
    IXMLElement inode = (IXMLElement) it.next();
    long id = inode.getAttribute("id", 0);
    System.out.println("id="+id);
 
   }
   

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

 }

}

上一篇:iOS 南京互联网大会分享及个人见解 韩俊强的博客


下一篇:Vue简单使用