服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java xml教程(附:源程序)


  sonnet.xml  这是在本教程中贯穿使用的示例 xml 文档。 <?xml version="1.0"?>  <!doctype sonnet system "sonnet.dtd">  <sonnet type="shakespearean">  <author>  <last-name>shakespeare</last-name>  <first-name>william</first-name>  <nationality>british</nationality>  <year-of-birth>1564</year-of-birth>  <year-of-death>1616</year-of-death>  </author>  <title>sonnet 130</title>  <text>  <line>my mistress` eyes are nothing like the sun,</line>  <line>coral is far more red than her lips red.</line>  <line>if snow be white, why then her breasts are dun,</line>  <line>if hairs be wires, black wires grow on her head.</line>  <line>i have seen roses damasked, red and white,</line>  <line>but no such roses see i in her cheeks.</line>  <line>and in some perfumes is there more delight</line>  <line>than in the breath that from my mistress reeks.</line>  <line>i love to hear her speak, yet well i know</line>  <line>that music hath a far more pleasing sound.</line>  <line>i grant i never saw a goddess go,</line>  <line>my mistress when she walks, treads on the ground.</line>  <line>and yet, by heaven, i think my love as rare</line>  <line>as any she belied with false compare.</line>  </text>  </sonnet>  sonnet.dtd  这是我们示例文档所用的 dtd。 <!-- sonnet.dtd -->  <!element sonnet (author,title?,text) >  <!attlist sonnet  type (shakespearean | petrarchan) "shakespearean">  <!element text (line,line,line,line,  line,line,line,line,  line,line,line,line,  line,line) >  <!element author (last-name,first-name,nationality,  year-of-birth?,year-of-death?) >  <!element title (<!element last-name (<!element first-name (<!element nationality (<!element year-of-birth (<!element year-of-death (<!element line ( domone.java  这是我们的第一个 dom 应用。它解析一个 xml 文档并将其内容输出到标准输出。 /*  * (c) copyright ibm corp. 1999 all rights reserved.  *  * us government users restricted rights use, duplication or  * disclosure restricted by gsa adp schedule contract with ibm corp.  *  * the program is provided "as is" without any warranty express or  * implied, including the warranty of non-infringement and the implied  * warranties of merchantibility and fitness for a particular purpose.  * ibm will not be liable for any damages suffered by you as a result  * of using the program. in no event will ibm be liable for any  * special, indirect or consequential damages or lost profits even if  * ibm has been advised of the possibility of their occurrence. ibm  * will not be liable for any third party claims against you.  */  import java.io.outputstreamwriter;  import java.io.printwriter;  import java.io.unsupportedencodingexception;  import org.w3c.dom.attr;  import org.w3c.dom.document;  import org.w3c.dom.namednodemap;  import org.w3c.dom.node;  import org.w3c.dom.nodelist;  import com.ibm.xml.parsers.*;  /**  * domone.java  * illustrates how to go through a dom tree.  */  public class domone  {  public void parseandprint(string uri)  {  document doc = null;  try  {  domparser parser = new domparser();  parser.parse(uri);  doc = parser.getdocument();  }  catch (exception e)  {  system.err.println("sorry, an error occurred: " + e);  }  // we`ve parsed the document now, so let`s print it.  if (doc != null)  printdomtree(doc);  }  /** prints the specified node, then prints all of its children. */  public void printdomtree(node node)  {  int type = node.getnodetype();  switch (type)  {  // print the document element  case node.document_node:  {  system.out.println("<?xml version="1.0" ?>");  printdomtree(((document)node).getdocumentelement());  break;  }  // print element with attributes  case node.element_node:  {  system.out.print("<");  system.out.print(node.getnodename());  namednodemap attrs = node.getattributes();  for (int i = 0; i < attrs.getlength(); i++)  {  node attr = attrs.item(i);  system.out.print(" " + attr.getnodename() +  "="" + attr.getnodevalue() +  """);  }  system.out.println(">");  nodelist children = node.getchildnodes();  if (children != null)  {  int len = children.getlength();  for (int i = 0; i < len; i++)  printdomtree(children.item(i));  }  break;  }  // handle entity reference nodes  case node.entity_reference_node:  {  system.out.print("&");  system.out.print(node.getnodename());  system.out.print(";");  break;  }  // print cdata sections  case node.cdata_section_node:  {  system.out.print("<![cdata[");  system.out.print(node.getnodevalue());  system.out.print("]]>");  break;  }  // print text  case node.text_node:  {  system.out.print(node.getnodevalue());  break;  }  // print processing instruction  case node.processing_instruction_node:  {  system.out.print("<?");  system.out.print(node.getnodename());  string data = node.getnodevalue(); 

扫描关注微信公众号