首页 > 解决方案 > 将 XML 转换为 Markdown 格式文本

问题描述

我正在尝试将 XML 转换为 Markdown

public static Document transformXmlToMarkdown(String xml) throws TransformerException {
    Source source = new StreamSource(new StringReader(xml));
    TransformerFactory tFactory=TransformerFactory.newInstance();
    DOMResult result = new DOMResult();
    Transformer transform = tFactory.newTransformer(new StreamSource(new StringReader(markdown)));
    transform.transform(source, result);
    return (Document) result.getNode();
}

markdown中的new StringReader(markdown)基于这里。这段代码的问题是我没有转换。

输入:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<en-note>
    <div>
        <br/>
    </div>
    <div>
        <br/>
    </div>
    <en-media hash="" type="application/octet-stream"/>
    <div>
        <br/>
    </div>
    <div>This is my first Evernote blog with image/photo attached.</div>
    <div>
        <br/>
    </div>
    <div>This is another line. </div>
    <div>
        <br/>
    </div>
    <div>Some 
        <i>formatting </i>also for 
        <b>some </b>lines. 
    </div>
</en-note>

输出应该是:

This is my first Evernote blog with image/photo attached.



This is another line.



Some _formatting_ also for **some** lines.

任何提示将不胜感激。

标签: javamarkdown

解决方案


您提到的 xslt 脚本可用于将某种 HTML 转换为 Markdown。

您的 XML 不是 HTML。您的 XML 以根元素“en-note”开头,并且在 XSLT 中没有匹配,因此不处理该元素。


推荐阅读