java解析XML

来源:互联网 发布:龙卷风优化软件官网 编辑:程序博客网 时间:2024/05/16 18:11

 <bookCollection>
<!-- 标题和集合名称 collection name -->
 <collectionName>My Tech Book Collection</collectionName>
 <collectionOwner>Lapo</collectionOwner>
  
 <!-- 书籍集合的名称 -->
 <bookList>
 <book title="Head First Design Patterns"
   author="Freeman, Freeman"
   year="2004"
   publisher="O'Reilly"
   />
  
 <book title="Thinking in Java"
   author="B.Eckel"
   year="2003"
   publisher="Prentice Hall"
   />
  
 <book title="C++ From ground up"
   author="H.Schildt"
   year="2004"
   publisher="McGraw-Hill Osborne Media"
   />
  
 <book title="Python in a Nutshell"
   author="A.Martelli"
   year="2003"
   publisher="O'Reilly"
   />
  
 <book title="C# Cookbook"
   author="Teilhet, Hilyard"
   year="2003"
   publisher="O'Reilly"
   />
 </bookList>
  
</bookCollection>

 

//java解析代码

import java.util.Enumeration;
import java.util.LinkedList;

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 XmlReaderExample
{
 IXMLParser xmlParser;  
 IXMLReader xmlReader;
   
 public XmlReaderExample()
 {
  IXMLElement book;
  
  try
  {
   // / /创建XML解析器
   xmlParser = XMLParserFactory.createDefaultXMLParser();
   xmlReader = StdXMLReader.fileReader("books.xml");
   xmlParser.setReader(xmlReader);
   
   // 读取文件,并剖析了!
   IXMLElement xmlDoc = (IXMLElement) xmlParser.parse();
   
   // 获取标签要求
   IXMLElement node = xmlDoc.getFirstChildNamed("collectionName");
   System.out.println("Collection Name: " + node.getContent());
   
   // Get the tag called
   node = xmlDoc.getFirstChildNamed("collectionOwner");
   System.out.println("Collection Owner: " + node.getContent());
   System.out.println("");
   
   // Get the tag called
   node = xmlDoc.getFirstChildNamed("bookList");
   
   Enumeration books = node.enumerateChildren();      
 
   LinkedList tempWordList = new LinkedList();
 
   while (books.hasMoreElements())
   {
       book = (IXMLElement) books.nextElement();
 
       System.out.println("---------------------------------------------------");
       System.out.println("Title    : " + book.getAttribute("title", ""));
       System.out.println("Author   : " + book.getAttribute("author", "unknown"));
       System.out.println("Year     : " + book.getAttribute("year", "unknown"));
       System.out.println("Publisher: " + book.getAttribute("publisher", "unknown"));
       System.out.println("---------------------------------------------------");
       System.out.println("");
   }
           
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  
 }
 
 public static void main (String[] args)
 {
  new XmlReaderExample();
 }


//as解析
function init()
{
       / /创建一个参考的Java包
         / /这有助于我们建立新的对象从nanoxml封装。
         / /而不是输入的完全合格的类的名字,我们只需使用:
        // var obj = new nanoxml.SomeObject()
        nanoxml = Packages.net.n3.nanoxml
       
        readTheXmlFile()
}

/*
* 读取并解析XML文件
*/
function readTheXmlFile()
{
        // 设置XML解析器对象
        var xmlParser = nanoxml.XMLParserFactory.createDefaultXMLParser()
       
        / /这是XML的读者:
         / /您可以使用fileReader ,来读取XML从档案
         / /或StringReader ,从一个字符串来读取XML
        var xmlReader = nanoxml.StdXMLReader.fileReader("sfsExtensions/data/books.xml")
       
        / /指定读者分析器
        xmlParser.setReader(xmlReader)
       
        / /最后的XML解析
        var xmlDoc = xmlParser.parse()
       
          / /获取标签要求<collectionName></collectionName>
        var node = xmlDoc.getFirstChildNamed("collectionName")
        trace("Collection Name: " + node.getContent())
       
         / /获取标签要求 <collectionOwner></collectionOwner>
        var node = xmlDoc.getFirstChildNamed("collectionOwner")
        trace("Collection Owner: " + node.getContent() + newline)
       
        / /获取标签要求<collectionOwner></collectionOwner>
        var node = xmlDoc.getFirstChildNamed("bookList")
       
        / /本书是一个java.util.Enumeration对象
        var books = node.enumerateChildren()
       
       / /循环通过每个元素的在这个枚举中
        //
        while (books.hasMoreElements())
        {
                var book = books.nextElement()
               
                trace("Title    : " + book.getAttribute("title", ""))
                trace("Author   : " + book.getAttribute("author", "unknown"))
                trace("Year     : " + book.getAttribute("year", "unknown"))
                trace("Publisher: " + book.getAttribute("publisher", "unknown"))
                trace("-------------------------------------")
        }
}

function destroy()
{
        //
}

function handleRequest(cmd, params, user, fromRoom)
{
       
        //
}

function handleInternalEvent(evt)
{
        //
}

 

getFirstChildNamed ( nodeName ) 搜索首次出现的一个分节点具有相同名称的通过论证
enumerateChildren ( )    返回一个列表的子节点
getAttribute (姓名, defValue )  返回的属性的值, defValue是默认值,属性没有设置的情况下
getValue ( )      返回节点值

 

原创粉丝点击