(Step 3)JDOM操作XML之(Sonnet示例)

本文介绍了一种使用Java JDOM库读取并输出sonnet.xml文件内容的方法。该示例展示了如何通过SAXBuilder构建Document对象,并利用XMLOutputter进行格式化输出,同时去除了所有不必要的空白字符。
部署运行你感兴趣的模型镜像

读取sonnet文件,然后以流的方式输出。注意,除了sonnet.xml还要定义sonnet.dtd文件。(关于xml的相关信息,在以后的文章中会涉及)

 

public class JdomOne
{
  public static void main(String[] argv)
  {
    try
    {
      SAXBuilder sb = new SAXBuilder();
      Document doc = sb.build(new File("sonnet.xml"));
      XMLOutputter xo = new XMLOutputter();

      xo.setTrimAllWhite(true); //去除所有空格回车
      xo.output(doc, System.out);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}

 

 

---sonnet.xml---

 

<?xml version="1.0"?>
<!DOCTYPE sonnet SYSTEM "sonnet.dtd">
<sonnet type="Shakespearean">
  <author>
    <lastName>Shakespeare</lastName>
    <firstName>William</firstName>
    <nationality>British</nationality>
    <yearOfBirth>1564</yearOfBirth>
    <yearOfDeath>1616</yearOfDeath>
  </author>
  <title>Sonnet 130</title>
  <lines>
    <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>
  </lines>

</sonnet>

 

 

---sonnet.dtd-----

 

<!-- sonnet.dtd                                        -->

<!-- sonnet is the root of the document                -->
<!ELEMENT sonnet  (author,title?,lines)>
<!-- the default sonnet type is "Shakespearean"        -->
<!ATTLIST sonnet  type (Shakespearean | Petrarchan) 
                       "Shakespearean">

<!-- author contains information about the author      -->
<!ELEMENT author  (lastName,firstName,nationality,
                   yearOfBirth?,yearOfDeath?)>

<!-- lastName, firstName, nationality, yearOfBirth,
     and yearOfDeath are all elements inside author. -->

<!ELEMENT lastName (#PCDATA)>
<!ELEMENT firstName (#PCDATA)>
<!ELEMENT nationality (#PCDATA)>
<!ELEMENT yearOfBirth (#PCDATA)>
<!ELEMENT yearOfDeath (#PCDATA)>

<!-- The title of the sonnet                           -->
<!ELEMENT title (#PCDATA)>

<!-- The lines element contains the 14 lines of the   
     sonnet.                                           -->
<!ELEMENT lines (line,line,line,line,
                 line,line,line,line,
                 line,line,line,line,
                 line,line)>

<!ELEMENT line (#PCDATA)>

 

您可能感兴趣的与本文相关的镜像

LobeChat

LobeChat

AI应用

LobeChat 是一个开源、高性能的聊天机器人框架。支持语音合成、多模态和可扩展插件系统。支持一键式免费部署私人ChatGPT/LLM 网络应用程序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值