在生成XML文件时,如何生成下面内容
<SCL xmlns=" http://www.iec.ch/61850/2003/SCL" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xmlns:schemaLocation=" http://www.iec.ch/61850/2003/SCL SCL.xsd">

解决方案 »

  1.   

    用转义:
     <>"/等都用转义的代替吧,
      

  2.   


    Document document = new Document();

    Namespace ns = Namespace.getNamespace("http://www.iec.ch/61850/2003/SCL");
    Namespace ns1 = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    Namespace ns2 = Namespace.getNamespace("schemaLocation","http://www.iec.ch/61850/2003/SCL SCL.xsd");

    Element scl = new Element("scl",ns);
    scl.addNamespaceDeclaration(ns1);
    scl.addNamespaceDeclaration(ns2);

    document.setContent(scl);

    try {
    FileWriter writer = new FileWriter(new File("d://jdomxml"));
    XMLOutputter o = new XMLOutputter();
    Format format = Format.getPrettyFormat();
    format.setEncoding("UTF-8");
    format.setIndent("");
    o.setFormat(format);
    o.output(document, writer);
    } catch (IOException e) {
    e.printStackTrace();
    }
      

  3.   

    不是吧,
    网上找下这些转义的符号呀,
    < = &lt;
    > = &gt;
    这里有http://114.xixik.com/character/
      

  4.   

            Namespace ns = Namespace.getNamespace("http://www.iec.ch/61850/2003/SCL");
            Namespace xsi = Namespace.getNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");
            Namespace location = Namespace.getNamespace("schemaLocation","http://www.iec.ch/61850/2003/SCL SCL.xsd");        Element root = new Element("SCL",ns);
            root.addNamespaceDeclaration(xsi);
            root.addNamespaceDeclaration(location);        Document document = new Document(root);
      

  5.   

    hi,brihgtyq.
    如果要生成的根节点是如下形式,该如何处理呢?
    <SCL xmlns=" http://www.iec.ch/61850/2003/SCL" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.iec.ch/61850/2003/SCL SCL.xsd">
      

  6.   

    Document document = new Document();

    Namespace ns = Namespace.getNamespace("http://www.iec.ch/61850/2003/SCL");
    Namespace ns1 = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    Element scl = new Element("scl",ns);
    scl.addNamespaceDeclaration(ns1);Attribute attr = new Attribute("schemaLocation","http://www.iec.ch/61850/2003/SCL SCL.xsd");
    attr.setNamespace(ns1);
    scl.setAttribute(attr);

    document.setContent(scl);