SYSTEM DEVELOPMENT

STUDY GROUP

javaでXMLを生成

業務でXMLを生成する必要ができたため復習しました。
作りたいXMLはこんな感じ。
<md:EntityDescriptor entityID=”https://test.com”>
<md:SPSSODescriptor protocolSupportEnumeration=”urn:oasis:names:tc:SAML:2.0:protocol”>
<md:NameIDFormat>
urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified
</md:NameIDFormat>
<md:AssertionConsumerService Binding=”urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST” Location=”https://test.com/saml/acs” index=”0″/>
</md:SPSSODescriptor>
</md:EntityDescriptor>
以下はソース。
package sample;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
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;
public class TestXMLGen {
public static void main(String[] args) {
// Documentインスタンスの生成
DocumentBuilder documentBuilder = null;
try {
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document document = documentBuilder.newDocument();
// XML文書の作成
Element entityDescriptor = document.createElement(“md:EntityDescriptor”);
entityDescriptor.setAttribute(“entityID”, “https://test.com”);
document.appendChild(entityDescriptor);
Element spssoDescriptor = document.createElement(“md:SPSSODescriptor”);
spssoDescriptor.setAttribute(“protocolSupportEnumeration”, “urn:oasis:names:tc:SAML:2.0:protocol”);
entityDescriptor.appendChild(spssoDescriptor);
Element nameIDFormat = document.createElement(“md:NameIDFormat”);
nameIDFormat.appendChild(document.createTextNode(“urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified”));
spssoDescriptor.appendChild(nameIDFormat);
Element assertionConsumerService = document.createElement(“md:AssertionConsumerService”);
assertionConsumerService.setAttribute(“Binding”, “urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST”);
assertionConsumerService.setAttribute(“Location”, “https://test.com/saml/acs”);
assertionConsumerService.setAttribute(“index”, “0”);
spssoDescriptor.appendChild(assertionConsumerService);
// XMLファイルの作成
File file = new File(“C:\\work\\tmp\\Test.xml”);
write(file, document);
}
public static boolean write(File file, Document document) {
// Transformerインスタンスの生成
Transformer transformer = null;
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
return false;
}
// Transformerの設定
transformer.setOutputProperty(“indent”, “yes”); //改行指定
transformer.setOutputProperty(“encoding”, “UTF-8”); // エンコーディング
// XMLファイルの作成
try {
transformer.transform(new DOMSource(document), new StreamResult(file));
} catch (TransformerException e) {
e.printStackTrace();
return false;
}
return true;
}
}
以上です。

menu