下面列出了javax.xml.transform.TransformerException#printStackTrace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Sends the response to the client.
* @param request The original request.
* @param response The response object we can use to send the data.
* @param respBody The XML document of the response.
* @return A response object for Spark
*/
protected Object sendResponse(final Request request, final Response response, final BaseXMLBuilder respBody) {
// get the bytes of the XML document
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
final Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
final DOMSource source = new DOMSource(respBody.getDocument());
final StreamResult result = new StreamResult(bos);
transformer.transform(source, result);
} catch (TransformerException e) {
e.printStackTrace();
return 500;
}
byte[] respBytes = bos.toByteArray();
return this.sendBytesToClient(respBytes, request, response);
}
/**
* the format should be something like this: [ ['My Workspace', null, ['Mary Morgan Folder', null, ['3 activity
* sequence','1024'] ], ['Organisations', null, ['Developers Playpen', null, ['Lesson Sequence Folder', null,
* ['',null] ] ], ['MATH111', null, ['Lesson Sequence Folder', null, ['',null] ] ] ] ] ]
*/
@Override
public String toString() {
// return '[' + convert() + ']';
Document document = getDocument();
try {
DOMSource domSource = new DOMSource(document);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
} catch (TransformerException ex) {
ex.printStackTrace();
return null;
}
}
protected String getStringFromDocument(Document doc) {
try {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
}
catch(TransformerException ex) {
ex.printStackTrace();
return null;
}
}
public String getXMLAsString() {
this.dom.appendChild(this.root);
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(dom), new StreamResult(writer));
return writer.toString();
} catch (TransformerException e) {
e.printStackTrace();
return null;
}
}
protected String getStringFromDocument(Document doc) {
try {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
}
catch(TransformerException ex) {
ex.printStackTrace();
return null;
}
}
protected String getStringFromDocument(Document doc) {
try {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
}
catch(TransformerException ex) {
ex.printStackTrace();
return null;
}
}
protected String getStringFromDocument(Document doc) {
try {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
}
catch(TransformerException ex) {
ex.printStackTrace();
return null;
}
}
public static String source2String(Source source) {
try
{
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(source, result);
writer.flush();
return writer.toString();
}
catch(TransformerException ex)
{
ex.printStackTrace();
return null;
}
}
protected String getStringFromDocument(Document doc) {
try {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
}
catch(TransformerException ex) {
ex.printStackTrace();
return null;
}
}
public Source invoke(Source request) {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
try {
transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
/*
tfactory.setAttribute("indent-number", "2");
*/
Transformer serializer = transformerFactory.newTransformer();
// Setup indenting to "pretty print"
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StringWriter swriter = new StringWriter();
serializer.transform(request, new StreamResult(swriter));
swriter.flush();
LOG.info("Provider received a request\n" + swriter.toString());
} catch (TransformerException e) {
e.printStackTrace();
}
return null;
}
protected String getStringFromDocument(Document doc) {
try {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
}
catch(TransformerException ex) {
ex.printStackTrace();
return null;
}
}
protected String getStringFromDocument(Document doc) {
try {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
}
catch(TransformerException ex) {
ex.printStackTrace();
return null;
}
}
public static Node selectNode(Element documentElement, String xpath)
{
Node res = null;
try {
Node result = XPathAPI.selectSingleNode(documentElement, xpath);
if (result!=null)
res = result;
} catch (TransformerException e) {
e.printStackTrace();
}
return res;
}
@Override
public void end() {
try {
DOMSource domSource = new DOMSource(doc);
StreamResult result = new StreamResult(output);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(domSource, result);
} catch (TransformerException e) {
e.printStackTrace();
}
}
public String transform( Value request )
throws FaultException {
try {
StreamSource source = new StreamSource( new StringReader( request.getFirstChild( "source" ).strValue() ) );
Transformer t = transformerFactory
.newTransformer( new StreamSource( new StringReader( request.getFirstChild( "xslt" ).strValue() ) ) );
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult( writer );
t.transform( source, result );
return writer.toString();
} catch( TransformerException e ) {
e.printStackTrace();
throw new FaultException( e );
}
}
public String nodeToString(Node node) {
StringWriter writer = new StringWriter();
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer xform;
try {
xform = tfactory.newTransformer();
xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Source src = new DOMSource(node);
Result result = new StreamResult(writer);
xform.transform(src, result);
} catch (TransformerException e) {
e.printStackTrace();
}
return writer.toString();
}
public static void saveDoc(Document doc, String fileName)
{
try
{
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(fileName));
transformer.transform(source, result);
} catch (TransformerException e)
{
e.printStackTrace();
}
}
static public Document cloneDocument(Document document) {
if (document == null) return null;
Document result = newDocument();
try {
identityTransformer.transform(new DOMSource( document), new DOMResult( result));
} catch (TransformerException e) {
e.printStackTrace();
}
return result;
}
public void run(){
while (true) {
UUID newfilterId = null;
int filterCount = 0;
synchronized (filterIdList) {
if (filterIdList.size() == 0) {
break;
}
newfilterId = filterIdList.remove(0);
}
if (newfilterId != null) {
try {
filterCount = getFilterCount(newfilterId, username);
} catch (TransformerException e) {
e.printStackTrace();
}
}
synchronized (filterIdList) {
filterDataCountMap.put(newfilterId, filterCount);
}
}
countDown.countDown();
}
public static void main(String argv[]) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(ClassLoader.getSystemResourceAsStream("file.xml"));
// Get the root element
//Node company = doc.getFirstChild();
// Get the staff element , it may not working if tag has spaces, or
// whatever weird characters in front...it's better to use
// getElementsByTagName() to get it directly.
// Node staff = company.getFirstChild();
// Get the staff element by tag name directly
Node staff = doc.getElementsByTagName("staff").item(0);
// update staff attribute
NamedNodeMap attr = staff.getAttributes();
Node nodeAttr = attr.getNamedItem("id");
nodeAttr.setTextContent("2");
// append a new node to staff
Element age = doc.createElement("age");
age.appendChild(doc.createTextNode("28"));
staff.appendChild(age);
// loop the staff child node
NodeList list = staff.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
// get the salary element, and update the value
if ("salary".equals(node.getNodeName())) {
node.setTextContent("2000000");
}
//remove firstname
if ("firstname".equals(node.getNodeName())) {
staff.removeChild(node);
}
}
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("target/file.xml"));
transformer.transform(source, result);
System.out.println("Done");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
}