久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何從 JDOM 獲取節點內容

How to get node contents from JDOM(如何從 JDOM 獲取節點內容)
本文介紹了如何從 JDOM 獲取節點內容的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在使用 import org.jdom.* 編寫一個 java 應用程序;

I'm writing an application in java using import org.jdom.*;

我的 XML 是有效的,但有時它包含 HTML 標記.例如,像這樣:

My XML is valid,but sometimes it contains HTML tags. For example, something like this:

  <program-title>Anatomy &amp; Physiology</program-title>
  <overview>
       <content>
              For more info click <a href="page.html">here</a>
              <p>Learn more about the human body.  Choose from a variety of Physiology (A&amp;P) designed for complementary therapies.&amp;#160; Online studies options are available.</p>
       </content>
  </overview>
  <key-information>
     <category>Health &amp; Human Services</category>

所以我的問題在于 <p > overview.content 節點內的標簽.

So my problem is with the < p > tags inside the overview.content node.

我希望這段代碼可以工作:

I was hoping that this code would work :

        Element overview = sds.getChild("overview");
        Element content = overview.getChild("content");

        System.out.println(content.getText());

但它返回空白.

如何從 overview.content 節點返回所有文本(嵌套標簽和所有)?

How do I return all the text ( nested tags and all ) from the overview.content node ?

謝謝

推薦答案

content.getText() 提供即時文本,該文本僅對帶有文本內容的葉子元素有用.

content.getText() gives immediate text which is only useful fine with the leaf elements with text content.

技巧是使用 org.jdom.output.XMLOutputter (帶文本模式 CompactFormat )

Trick is to use org.jdom.output.XMLOutputter ( with text mode CompactFormat )

public static void main(String[] args) throws Exception {
    SAXBuilder builder = new SAXBuilder();
    String xmlFileName = "a.xml";
    Document doc = builder.build(xmlFileName);

    Element root = doc.getRootElement();
    Element overview = root.getChild("overview");
    Element content = overview.getChild("content");

    XMLOutputter outp = new XMLOutputter();

    outp.setFormat(Format.getCompactFormat());
    //outp.setFormat(Format.getRawFormat());
    //outp.setFormat(Format.getPrettyFormat());
    //outp.getFormat().setTextMode(Format.TextMode.PRESERVE);

    StringWriter sw = new StringWriter();
    outp.output(content.getContent(), sw);
    StringBuffer sb = sw.getBuffer();
    System.out.println(sb.toString());
}

輸出

For more info click<a href="page.html">here</a><p>Learn more about the human body. Choose from a variety of Physiology (A&amp;P) designed for complementary therapies.&amp;#160; Online studies options are available.</p>

請探索其他 格式化 選項并在上面進行修改根據您的需要編寫代碼.

Do explore other formatting options and modify above code to your need.

封裝XMLOutputter格式選項的類.典型用戶可以使用getRawFormat()(不改變空白)、getPrettyFormat()(空白美化)、getCompactFormat()(空白歸一化)得到的標準格式配置."

"Class to encapsulate XMLOutputter format options. Typical users can use the standard format configurations obtained by getRawFormat() (no whitespace changes), getPrettyFormat() (whitespace beautification), and getCompactFormat() (whitespace normalization). "

這篇關于如何從 JDOM 獲取節點內容的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Upload progress listener not fired (Google drive API)(上傳進度偵聽器未觸發(Google 驅動器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅動api服務賬戶查看上傳文件到谷歌驅動使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務帳號返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 国产精品婷婷 | 日日噜噜噜夜夜爽爽狠狠视频97 | 精品网| 日韩高清成人 | 男女羞羞视频在线看 | 国产一区视频在线 | av看片网| 国产ts人妖系列高潮 | 久久香蕉精品视频 | 亚洲综合一区二区三区 | 国产精品地址 | 国产一级久久久久 | 一级毛毛片 | 国产精品视频网址 | 国产精品视频www | 国产91网址 | 日韩中文字幕在线观看 | 97精品国产一区二区三区 | 婷婷久久综合 | 婷婷桃色网 | 国产第一区二区 | 午夜丰满寂寞少妇精品 | 久操国产 | 国产激情在线 | 亚洲欧洲成人av每日更新 | 成人免费视频在线观看 | 狠狠亚洲 | 一级片aaa| 91精品欧美久久久久久久 | 国产伦精品一区二区三区视频金莲 | 午夜二区 | 国产精品久久久久久久久久不蜜臀 | 日本久久一区 | 一本一道久久a久久精品蜜桃 | 91亚洲精品在线观看 | 亚洲免费影院 | 久久久久99 | 一区二区av | 国产成人精品午夜视频免费 | 国产精品亚洲欧美日韩一区在线 | 久久成人免费视频 |