0

解決しました!

xml形式についてお聞きしたいです。以下のコード、間違いはありましたか?

import java.io.File;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.ProcessingInstruction;

public class createXml {
public static void main(String[] args) throws Exception {
    try {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        doc.setXmlStandalone(true);
        ProcessingInstruction pi = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"../xslt/kebir.xslt");
        // OgrenciBilgiler Oluşturuldu
        Element root = doc.createElement("xbrli:xbrl");
        doc.appendChild(root);
        doc.insertBefore(pi, root);
        root.setAttribute("xmlns:xbrli", "http://www.xbrl.org/2003/instance");
        root.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");

        // <Numara> etiketi yaratma
        Element element1 = doc.createElement("link:schemaRef");
        root.appendChild(element1);
        element1.setAttribute("xlink:href", "../xsd/2006-10-25/plt/case-c-b/gl-plt-2006-10-25.xsd");
        element1.setAttribute("xlink:type", "simple");


        // <Isim> etiketi yaratma
        Element ogrenciIsim = doc.createElement("Isim");
        root.appendChild(ogrenciIsim);
        //içerik girişi
        ogrenciIsim.setTextContent("Ali");


        // <Sınıfı> Etiketi
        Element ogrenciSinif = doc.createElement("Sinifi");
        root.appendChild(ogrenciSinif);
        //içerik girişi
        ogrenciSinif.setTextContent("10/A");

        doc.normalize();
        //dokümanı kapatma

        // output formaı ayarlama
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        // print xml-Document
        transformer.transform(new DOMSource(doc), new StreamResult(new File("D:/eclipse - galileo/workspace/hhs/xml/file.xml")));

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

コードの出力:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../xslt/kebir.xslt?>

<xbrli:xbrl xmlns:xbrli="http://www.xbrl.org/2003/instance" xmlns:xlink="http://www.w3.org/1999/xlink">
<link:schemaRef xlink:href="../xsd/2006-10-25/plt/case-c-b/gl-plt-2006-10-25.xsd" xlink:type="simple"/>
<Isim>Ali</Isim>
<Sinifi>10/A</Sinifi>
</xbrli:xbrl>

しかし、この出力は次のようになります。

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../xslt/kebir.xslt?>

<xbrli:xbrl xmlns:xbrli="http://www.xbrl.org/2003/instance" xmlns:xlink="http://www.w3.org/1999/xlink">
     <link:schemaRef xlink:href="../xsd/2006-10-25/plt/case-c-b/gl-plt-2006-10-25.xsd" xlink:type="simple"/>
     <Isim>Ali</Isim>
     <Sinifi>10/A</Sinifi>
</xbrli:xbrl>

XML pretty format を検索しましたが、問題の解決策が見つかりませんでした。返信ありがとうございます。

4

0 に答える 0