10 Nisan 2018 Salı

Transformer Sınıfı

constructor
TransformerFactory nesnesi tarafından oluşturulur. Şöyle yaparız.
Transformer transformer = TransformerFactory.newInstance().newTransformer();
setOutputProperty metodu
Şöyle yaparız.
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transform metodu - StringSource + StreamResult 
Örnek
Şöyle yaparız.
String myXML="<mytext>hiii</mytext>";
SoapMessage soapMessage = (SoapMessage) messageContext.getRequest();
SoapHeader soapHeader = soapMessage.getEnvelope().getHeader();
Transformer transformer = ...;
StringSource s = new StringSource(myXML);
transformer.transform(s, soapHeader.getResult());
transform metodu - DOMSource  + StreamResult 
Örnek
Şöyle yaparız.
DOMSource source = ...;
StreamResult result = ...;
transformer.transform(source, result);
Örnek
Şöyle yaparız.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("C:/Desktop/test.xml"));  

Node nList = doc.getDocumentElement().getChildNodes().item(0);

Element newserver=doc.createElement("field");
newserver.setAttribute("source", "33");
nList.appendChild(newserver).normalize();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(doc);          
StreamResult result = new StreamResult(new File("C:/Desktop/test.xml")); 
transformer.transform(source, result);





Hiç yorum yok:

Yorum Gönder