xiuyuan123 发表于 2006-5-26 11:33

[原创]根据dtd读写xml实例

<P>package franzy;</P>
<P>import org.w3c.dom.Element;<BR>import oracle.xml.parser.v2.XMLElement;<BR>import oracle.xml.parser.v2.XMLDocument;<BR>//import com.biztunnel.util.tools.ToolKit;</P>
<P>public class Agency<BR>{<BR>    private String agencyID;<BR>    private String agencyOther;<BR>    private String a_dtype;<BR>    private Element element;</P>
<P>    public static final String AGENCYCODE_OTHER = "Other";<BR>    public static final String AGENCYCODE_COMMERCEONE = "CommerceOne";<BR>    public static final String AGENCYCODE_ISO = "ISO";<BR>    public static final String AGENCYCODE_EAN = "EAN";<BR>    public static final String AGENCYCODE_ASSIGNEDBYSUPPLIER = "AssignedBySupplier";<BR>    public static final String AGENCYCODE_ASSIGNEDBYBUYER = "AssignedByBuyer";<BR>    public static final String AGENCYCODE_ANSI = "ANSI";<BR>    public static final String AGENCYCODE_GBABA = "GBABA";<BR>    private static final String AGENCY_ATTRIBUTE_A_DTYPE="%Agency-Dtypes;";</P>
<P>    /**<BR>     * Creates a Agency Element according to the outer element<BR>     */<BR>    private void init()<BR>    {<BR>        this.element=new XMLElement("Agency");<BR>        agencyID = null;<BR>        agencyOther = null;<BR>        a_dtype = this.AGENCY_ATTRIBUTE_A_DTYPE;<BR>    }</P>
<P>    public Agency(Element newElement)<BR>    {<BR>        init();</P>
<P>        //Element newElement = (Element)newElement1.getLastChild();<BR>        if(!newElement.getNodeName().equals("Agency"))<BR>        //if(!newElement1.getLastChild().getNodeName().equals("Agency"))<BR>        {<BR>            System.out.println("not it!!!");</P>
<P>        }<BR>        //get attribute<BR>        this.setAgencyID(newElement.getAttribute("AgencyID "));<BR>        this.setAgencyOther(newElement.getAttribute("AgencyOther "));<BR>        this.agencyID = newElement.getAttribute("a-dtype");<BR>    }</P>
<P>    /**<BR>     * Create an empty Agency element according to the buginfo.dtd<BR>     */<BR>    public Agency()<BR>    {<BR>        //initialize the empty node<BR>        this.init();<BR>    }</P>
<P>    /**<BR>     * convert this element to W3c Element format<BR>     */<BR>    public Element getElement()<BR>    {<BR>      if(this.getAgencyID()!=null)<BR>      {<BR>          this.element.setAttribute("AgencyID",this.getAgencyID());<BR>      }<BR>      if(this.getAgencyOther()!=null)<BR>      {<BR>          this.element.setAttribute("AgencyOther",this.agencyOther);<BR>      }<BR>      if(this.getA_dtype()!=null)<BR>      {<BR>          this.element.setAttribute("a-dtype",this.a_dtype);<BR>      }<BR>      return this.element;<BR>    }</P>
<P><BR>    public void setAgencyID(String newAgencyID)<BR>    {<BR>        agencyID = newAgencyID;<BR>    }</P>
<P>    public String getAgencyID()<BR>    {<BR>        return agencyID;<BR>    }</P>
<P>    public void setAgencyOther(String newAgencyOther)<BR>    {<BR>        agencyOther = newAgencyOther;<BR>    }</P>
<P>    public String getAgencyOther()<BR>    {<BR>        return agencyOther;<BR>    }</P>
<P>    public String getA_dtype()<BR>    {<BR>        return a_dtype;<BR>    }</P>
<P>    public void setFixedA_dtype(boolean isSet)<BR>    {<BR>        if(isSet)<BR>        {<BR>            a_dtype = this.AGENCY_ATTRIBUTE_A_DTYPE;<BR>        }<BR>        else<BR>        {<BR>            a_dtype = null;<BR>        }<BR>    }</P>
<P>    public static void main(String[] args)<BR>    {<BR>        try<BR>        {<BR>            //test 1<BR>            Agency element= new Agency();<BR>            element.setFixedA_dtype(true);<BR>            element.setAgencyID(Agency.AGENCYCODE_ASSIGNEDBYBUYER);<BR>            element.setAgencyOther("other");<BR>            XMLDocument doc = new XMLDocument();<BR>            doc.appendChild(element.getElement());<BR>            System.out.println(ToolKit.getXmlString(doc));</P>
<P>            //test 2<BR>            Agency element1= new Agency(element.getElement());<BR>            System.out.println(element1.getA_dtype());<BR>        }catch(Exception e)<BR>        {<BR>            e.printStackTrace();<BR>        }</P>
<P>    }<BR>}</P>
<P>有问题大家来讨论</P>

labid 发表于 2006-6-30 02:12

你是用C#在写`?

lldgood 发表于 2006-7-18 10:09

是j2ee技术的吧?

页: [1]

编程论坛