有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将XML转换为标记格式文本

我正在尝试将XML转换为降价

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();
}

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

输入:

<?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.

如有任何提示,将不胜感激


共 (1) 个答案

  1. # 1 楼答案

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

    你的XML不是HTML。XML以根元素“en note”开头,XSLT中没有与之匹配的元素,因此没有对该元素的处理