首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

jwsdp解析xml 范例-转载

2012-09-04 
jwsdp解析xml 实例-转载1、安装jwsdp-2_0-windows-i586.exe2、设置jwsdp环境变量 C:\jwsdp-2.0 安装目录 set

jwsdp解析xml 实例-转载
1、安装jwsdp-2_0-windows-i586.exe

2、设置jwsdp环境变量
C:\jwsdp-2.0 安装目录

set JWSDP_HOME=C:\jwsdp-2.0
set JWSDP_JAXB_LIB=%JWSDP_HOME%\jaxb\lib
set JWSDP_SHARED_LIB=%JWSDP_HOME%\jwsdp-shared\lib
set JWSDP_SJSXP_LIB=%JWSDP_HOME%\sjsxp\lib

set CLASSPATH=%JWSDP_JAXB_LIB%\jaxb-api.jar;
              %JWSDP_JAXB_LIB%\jaxb-impl.jar;
              %JWSDP_JAXB_LIB%\jaxb1-impl.jar;
              %JWSDP_JAXB_LIB%\jaxb-xjc.jar;%CLASSPATH%

set CLASSPATH=%JWSDP_SJSXP_LIB%\sjsxp.jar;
              %JWSDP_SJSXP_LIB%\jsr173_api.jar;%CLASSPATH%

set CLASSPATH=%JWSDP_SHARED_LIB%\activation.jar;
              %JWSDP_SHARED_LIB%\resolver.jar;%CLASSPATH%
set PATH=%JWSDP_HOME%\jaxb\bin;%JWSDP_HOME%\jwsdp-shared\bin;%PATH%


3、创建工程

   在工程src目录下建bookxsd.xsd 与 books.xml

books.xml-------------------------------------

   <?xml version="1.0" ?>
<Collection>
<books>
<book itemId="999">
  <name>Learning JAXB</name>
  <ISBN>123445</ISBN>
  <price>34 $</price>
<authors>
  <authorName>Jane Doe</authorName>
  </authors>
  <description>This books contains step by step instructions for beginners so that they can start using Java API for XML Binding.</description>
<promotion>
  <Discount>10% on this book if purchased by March 2003</Discount>
  </promotion>
  <publicationDate>2003-01-01</publicationDate>
  <bookCategory>other</bookCategory>
  </book>
<book itemId="129">
  <name>Java Webservices today and Beyond</name>
  <ISBN>522965</ISBN>
  <price>29 $</price>
<authors>
  <authorName>John Brown</authorName>
  <authorName>Peter T.</authorName>
  </authors>
  <description>This books contains information for users so that they can start using Java Web Services Developer Pack.</description>
<promotion>
  <Discount>Buy one get Learning webservices Part 1 free</Discount>
  </promotion>
  <publicationDate>2002-11-01</publicationDate>
  <bookCategory>magazine</bookCategory>
  </book>
  </books>
  </Collection>
 
bookxsd.xsd-------------------------------
<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="1.0">
<xs:element name="Collection">
<xs:complexType>
<xs:sequence>
<xs:element name="books">
<xs:complexType>
<xs:sequence>
  <xs:element name="book" type="bookType" minOccurs="1" maxOccurs="unbounded" />
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:sequence>
  </xs:complexType>
  </xs:element>
<xs:complexType name="bookType">
<xs:sequence>
  <xs:element name="name" type="xs:string" />
  <xs:element name="ISBN" type="xs:long" />
  <xs:element name="price" type="xs:string" />
<xs:element name="authors">
<xs:complexType>
<xs:sequence>
  <xs:element name="authorName" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  <xs:element name="description" type="xs:string" minOccurs="0" />
<xs:element name="promotion">
<xs:complexType>
<xs:choice>
  <xs:element name="Discount" type="xs:string" />
  <xs:element name="None" type="xs:string" />
  </xs:choice>
  </xs:complexType>
  </xs:element>
  <xs:element name="publicationDate" type="xs:date" />
<xs:element name="bookCategory">
<xs:simpleType>
<xs:restriction base="xs:NCName">
  <xs:enumeration value="magazine" />
  <xs:enumeration value="novel" />
  <xs:enumeration value="fiction" />
  <xs:enumeration value="other" />
  </xs:restriction>
  </xs:simpleType>
  </xs:element>
  </xs:sequence>
  <xs:attribute name="itemId" type="xs:string" />
  </xs:complexType>
<xs:simpleType name="bookCategoryType">
<xs:restriction base="xs:string">
  <xs:enumeration value="magazine" />
  <xs:enumeration value="novel" />
  <xs:enumeration value="fiction" />
  <xs:enumeration value="other" />
  </xs:restriction>
  </xs:simpleType>
  </xs:schema>



4、生成java文件

    在工程的根目录下执行命令
    D:\workspace\JAXBDemo>xjc -p com.ce -d src src/bookxsd.xsd
    生成4个java文件:
com\ce\BookCategoryType.java
com\ce\BookType.java
com\ce\Collection.java
com\ce\ObjectFactory.java

5、写测试类(解析xml)
import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.ce.BookType;
import com.ce.Collection;
import com.ce.Collection.Books;

public class JAXBTest {

public static void main(String[] args) throws JAXBException {
  JAXBContext jc = JAXBContext.newInstance("com.ce");
  Unmarshaller unmarshaller = jc.createUnmarshaller();
  Collection collection= (Collection)unmarshaller.unmarshal(new File( "src/books.xml"));
  Books books = collection.getBooks();
  List<BookType> bookList = books.getBook();
  for (BookType bookType : bookList) {
   System.out.println(bookType.getName());
  }
}
}

热点排行