Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Performance
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Performance
Create XML and Convert it to String
chicha sonu
Ranch Hand
Posts: 87
posted 6 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Is their any way to Increase Performance in the Following Process of Preparing the XMl and Convert it to
String
As some time it takes more than 50ms .
[b]import org.w3c.dom.Document; import org.w3c.dom.Element; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.XPathFactory; import java.io.StringWriter; public final class MessageUtil { private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY; private static final XPathFactory X_PATH_FACTORY; private static final TransformerFactory TRANSFORMER_FACTORY; static { DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance(); X_PATH_FACTORY = XPathFactory.newInstance(); TRANSFORMER_FACTORY = TransformerFactory.newInstance(); } public static String xmlGeneration(MyObject myObject) throws ParserConfigurationException, TransformerException { Document doc; DocumentBuilder documentBuilder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder(); doc = documentBuilder.newDocument(); Element element = doc.createElement("mydata2"); doc.appendChild(element); elementCreation("Param1", myObject.getParam1(), doc, element); elementCreation("param2", myObject.getParam2(), doc, element); String finalString = convertXmltoString(doc); return finalString; } private static void elementCreation(String name, Object value, Document doc, Element data) { if (null != value) { Element varElement = doc.createElement("data1"); Element nameElement = doc.createElement("data2"); nameElement.appendChild(doc.createTextNode(name)); Element valueElement = doc.createElement("data3"); valueElement.appendChild(doc.createTextNode(value.toString())); varElement.appendChild(nameElement); varElement.appendChild(valueElement); data.appendChild(varElement); } } private static String convertXmltoString(Document doc) throws TransformerException { Transformer transformer = TRANSFORMER_FACTORY.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter stringWriter = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(stringWriter)); return stringWriter.getBuffer().toString(); } public static void main(String[] args) throws TransformerException, ParserConfigurationException { MyObject myObject = new MyObject(); myObject.setParam1("param1"); myObject.setParam2("param2"); for(int i=0;i<10000;i++){ long startTime = System.currentTimeMillis(); xmlGeneration(myObject); System.out.println(System.currentTimeMillis()-startTime); } } }[/b]
Campbell Ritchie
Marshal
Posts: 79177
377
posted 6 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Please supply more
details
, including the location and size of your XML file. Have you done any profiling? Do you have lots of reading from the disc?
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
how to append nodes to the xml document
XSL file in memory
problem while appending child nodes
Order of Attributes in a Node
xml error_please help
More...