加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使JAXB生成XML处理指令

发布时间:2020-12-16 07:57:21 所属栏目:百科 来源:网络整理
导读:我正在使用JAXB动态生成XML. 现在,我想使用XSL将其转换为HTML.我怎么能包括 ?xml-stylesheet type="text/xsl" href="" 在动态生成的XML中? 这里的所有解决方案都非常丑陋且冗长.只需在Mashaller对象中设置指定附加标题的行. Marshaller jaxbMarshaller = ..
我正在使用JAXB动态生成XML.

现在,我想使用XSL将其转换为HTML.我怎么能包括

<?xml-stylesheet type="text/xsl" href="">

在动态生成的XML中?

这里的所有解决方案都非常丑陋且冗长.只需在Mashaller对象中设置指定附加标题的行.
Marshaller jaxbMarshaller = ...
jaxbMarshaller.setProperty("com.sun.xml.bind.xmlHeaders","<?xml-stylesheet type='text/xsl' href='nameoffile.xsl' ?>");

此示例将使用样式表将XML对象输出到文件,并且很好地格式化元素以供人类阅读.对象myXmlObject属于MyXmlClass类,将被写入文件,由xslUrl给出的样式表格式化:

JAXBContext context = JAXBContext.newInstance(MyXmlClass.class);
Marshaller marshaller = context.createMarshaller();
//Need to use a Writer to marshal with the XSL
FileWriter fw = new FileWriter(file);
//Do this or else the XML is all one line and not human friendly...
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
marshaller.setProperty("com.sun.xml.bind.xmlHeaders","<?xml-stylesheet type='text/xsl' href="" +
        xslUrl +
        "" ?>");
marshaller.marshal(myXmlObject,fw);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读