如何从JDOM获取节点内容
我正在使用import org.jdom在java中编写应用程序。*;
我的XML有效,但有时它包含HTML标记。 例如,像这样:
Anatomy & Physiology For more info click here Learn more about the human body. Choose from a variety of Physiology (A&P) designed for complementary therapies. Online studies options are available.
Health & Human Services
所以我的问题是overview.content节点内的
标签。
我希望这段代码能够正常工作:
Element overview = sds.getChild("overview"); Element content = overview.getChild("content"); System.out.println(content.getText());
但它返回空白。
如何从overview.content节点返回所有文本(嵌套标签和所有文本)?
谢谢
content.getText()
提供即时文本,只对带有文本内容的叶元素有用。
诀窍是使用org.jdom.output.XMLOutputter
(使用文本模式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 clickhereLearn more about the human body. Choose from a variety of Physiology (A&P) designed for complementary therapies.  Online studies options are available.
请探索其他格式选项并根据需要修改上述代码。
“封装XMLOutputter格式选项的类。典型用户可以使用getRawFormat()获取的标准格式配置(无空格更改),getPrettyFormat()(空白美化)和getCompactFormat()(空格规范化)。”
您可以尝试使用方法getValue()进行最接近的近似,但这样做可以将元素中的所有文本和后代连接在一起。 这不会以任何forms提供
标签。 如果该标记在您所显示的XML中,则它已成为XML标记的一部分。 它需要被包括为<p>
或嵌入CDATA部分,作为文本处理。
或者,如果您知道XML中可能出现或未出现的所有元素,则可以应用XSLT转换,将不作为标记的内容转换为纯文本。
好吧,也许这就是你需要的:
import java.io.StringReader; import org.custommonkey.xmlunit.XMLTestCase; import org.custommonkey.xmlunit.XMLUnit; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; import org.testng.annotations.Test; import org.xml.sax.InputSource; public class HowToGetNodeContentsJDOM extends XMLTestCase { private static final String XML = "\n" + " Anatomy & Physiology \n" + " \n" + " \n" + " For more info click here\n" + " Learn more about the human body. Choose from a variety of Physiology (A&P) designed for complementary therapies.  Online studies options are available.
\n" + " \n" + " \n" + " \n" + " Health & Human Services \n" + " \n" + " "; private static final String EXPECTED = "For more info click here\n" + "Learn more about the human body. Choose from a variety of Physiology (A&P) designed for complementary therapies.  Online studies options are available.
"; @Test public void test() throws Exception { XMLUnit.setIgnoreWhitespace(true); Document document = new SAXBuilder().build(new InputSource(new StringReader(XML))); List content = document.getRootElement().getChild("overview").getChild("content").getContent(); String out = new XMLOutputter().outputString(content); assertXMLEqual("" + EXPECTED + " ", "" + out + " "); } }
输出:
PASSED: test on instance null(HowToGetNodeContentsJDOM) =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 ===============================================
我正在使用JDom和generics: http ://www.junlu.com/list/25/883674.html
编辑:事实上,这与Prashant Bhate的回答并没有太大的不同。 也许你需要告诉我们你错过了什么……
如果您还要生成XML文件,则应该能够将您的html数据封装在以便XML解析器不会对其进行解析。
问题是
节点没有文本子节点; 它有一个恰好包含文本的
子项。
尝试这个:
Element overview = sds.getChild("overview"); Element content = overview.getChild("content"); Element p = content.getChild("p"); System.out.println(p.getText());
如果您想要所有直接子节点,请调用p.getChildren()
。 如果要获取所有子节点,则必须递归调用它。
不是特别漂亮,但工作正常(使用JDOM API):
public static String getRawText(Element element) { if (element.getContent().size() == 0) { return ""; } StringBuffer text = new StringBuffer(); for (int i = 0; i < element.getContent().size(); i++) { final Object obj = element.getContent().get(i); if (obj instanceof Text) { text.append( ((Text) obj).getText() ); } else if (obj instanceof Element) { Element e = (Element) obj; text.append( "<" ).append( e.getName() ); // dump all attributes for (Attribute attribute : (List)e.getAttributes()) { text.append(" ").append(attribute.getName()).append("=\"").append(attribute.getValue()).append("\""); } text.append(">"); text.append( getRawText( e )).append("").append(e.getName()).append(">"); } } return text.toString(); }
Prashant Bhate的解决方案虽然更好!
如果要输出某些JSOM节点的内容,请使用
System.out.println(new XMLOutputter().outputString(node))