jdom生1.0成xml

目标:

<?xml version="1.0" encoding="UTF-8"?>
<xrsi:mobile-money-send-validation-request 
xsi:schemaLocation=”http://www.westernunion.com/schema/xrsi
XRSIMobileSchema.xsd “
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xrsi="http://www.westernunion.com/schema/xrsi">
[ transaction contents here ]
</xrsi:mobile-money-send-validation-request>

 

schemaLocation 属性用来引用(schema)模式文档,解析器可以在需要的情况下使用这个文档对 XML 实例文档进行校验。它的值(URI)是成对出现的,第一个值表示命名空间,第二个值则表示描述该命名空间的模式文档的具体位置,两个值之间以空格分隔。

 

 

public class CreateXML {

  public void Create() {

   try {

    Document doc = new Document();  

    ProcessingInstruction pi=new ProcessingInstruction("xml-stylesheet","type="text/xsl" href="test.xsl"");

    doc.addContent(pi);   

    Namespace ns = Namespace.getNamespace("http://www.bromon.org" );

    Namespace ns2 = Namespace.getNamespace("other", "http://www.w3c.org" );

    Element root = new Element("根元素", ns);

    root.addNamespaceDeclaration(ns2);

    doc.setRootElement(root);

    Element el1 = new Element("元素一");

    el1.setAttribute("属性", "属性一");   

    Text text1=new Text("元素值");

             Element em = new Element("元素二").addContent("第二个元素");

    el1.addContent(text1);

             el1.addContent(em);            

             Element el2 = new Element("元素三").addContent("第三个元素");

             root.addContent(el1);

             root.addContent(el2);            

             //缩进四个空格,自动换行,gb2312编码

             XMLOutputter outputter = new XMLOutputter();

             outputter.output(doc, new FileWriter("test.xml"));

         }catch(Exception e)  {

          System.out.println(e);

         }

     }    

     public static void main(String args[]) {

      new CreateXML().Create();

     }    

 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值