<p>I am using Java back end for creating an XML string which is passed to the browser. Currently I am using simple string manipulation to produce this XML. Is it essential that I use some XML library in Java to produce the XML string?
I find the libraries very difficult to use compared to what I need.</p>

[{'answer_id': 24867, 'author': 'Abhinav', 'author_id': 2617, 'author_profile': 'https://Stackoverflow.com/users/2617', 'pm_score': 1, 'selected': False, 'text': '<p>No - If you can parse it yourself (as you are doing), and it will scale for your needs, you do not need any library.</p>\n\n<p>Just ensure that your future needs are going to be met - complex xml creation is better done using libraries - some of which come in very simple flavors too.</p>\n'}, {'answer_id': 24869, 'author': 'On Freund', 'author_id': 2150, 'author_profile': 'https://Stackoverflow.com/users/2150', 'pm_score': 2, 'selected': False, 'text': "<p>I think that custom string manipulation is fine, but you have to keep two things in mind:</p>\n\n<ol>\n<li>Your code isn't as mature as the library. Allocate time in your plan to handle the bugs that pop-up.</li>\n<li>Your approach will probably not scale as well as a 3rd party library when the xml starts to grow (both in terms of performance and ease of use).</li>\n</ol>\n\n<p>I know a code base that uses custom string manipulation for xml output (and a 3rd party library for input). It was fine to begin with but became a real hassle after a while.</p>\n"}, {'answer_id': 24872, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 2, 'selected': False, 'text': '<p>Yes, use the library.</p>\n\n<p>Somebody took the time and effort to create something that is usually better than what you could come up with. String manipulation is for sending back a single node, but once you start needing to manipulate the DOM, or use an XPath query, the library will save you.</p>\n'}, {'answer_id': 24894, 'author': 'Michael Neale', 'author_id': 699, 'author_profile': 'https://Stackoverflow.com/users/699', 'pm_score': 0, 'selected': False, 'text': '<p>No - especially for generating (parsing I would be less inclined to as input text can always surprise you). I think its fine - but be prepared to shift to a library should you find yourself spending more then a few minutes maintaining your own code. </p>\n'}, {'answer_id': 24897, 'author': 'Telcontar', 'author_id': 518, 'author_profile': 'https://Stackoverflow.com/users/518', 'pm_score': 0, 'selected': False, 'text': '<p>I don\'t think that using the DOM XML API wich comes with the JDK is difficult, it\'s easy to create Element nodes, attributes, etc... and later is easy convert strings to a DOM document sor DOM documents into a String</p>\n\n<p>In the first page google finds from Spain (<a href="http://soaagenda.com/journal/articulos/document-a-string/" rel="nofollow noreferrer">spanish XML example</a>):</p>\n\n<pre><code>public String DOM2String(Document doc)\n{\n    TransformerFactory transformerFactory =TransformerFactory.newInstance();\n    Transformer transformer = null;\n    try{\n        transformer = transformerFactory.newTransformer();\n    }catch (javax.xml.transform.TransformerConfigurationException error){\n        coderror=123;\n        msgerror=error.getMessage();\n        return null;\n    }\n\n    Source source = new DOMSource(doc);\n\n    StringWriter writer = new StringWriter();\n    Result result = new StreamResult(writer);\n    try{\n        transformer.transform(source,result);\n    }catch (javax.xml.transform.TransformerException error){\n        coderror=123;\n        msgerror=error.getMessage();\n        return null;\n    }\n\n    String s = writer.toString();\n    return s;\n}\n\npublic Document string2DOM(String s)\n{\n    Document tmpX=null;\n    DocumentBuilder builder = null;\n    try{\n        builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();\n    }catch(javax.xml.parsers.ParserConfigurationException error){\n        coderror=10;\n        msgerror="Error crando factory String2DOM "+error.getMessage();\n        return null;\n    }\n    try{\n        tmpX=builder.parse(new ByteArrayInputStream(s.getBytes()));\n    }catch(org.xml.sax.SAXException error){\n        coderror=10;\n        msgerror="Error parseo SAX String2DOM "+error.getMessage();\n        return null;\n    }catch(IOException error){\n        coderror=10;\n        msgerror="Error generando Bytes String2DOM "+error.getMessage();\n        return null;\n    }\n    return tmpX;\n}\n</code></pre>\n'}, {'answer_id': 24902, 'author': 'Euro Micelli', 'author_id': 2230, 'author_profile': 'https://Stackoverflow.com/users/2230', 'pm_score': 5, 'selected': True, 'text': '<p>It\'s not essential, but advisable. However, if string manipulation works for you, then go for it! There are plenty of cases where small or simple XML text can be safely built by hand.</p>\n\n<p>Just be aware that creating XML text is harder than it looks. Here\'s some criteria I would consider:</p>\n\n<ul>\n<li>First: how much <em>control</em> do you have on the information that goes into the xml?</li>\n</ul>\n\n<p>The less control you have on the source data, the more likely you will have trouble, and the more advantageous the library becomes. For example: (a) Can you <em>guarantee</em> that the element names will never have a character that is illegal in a name? (b) How about quotes in an attribute\'s content? Can they happen, and are you handling them? (c) Does the data ever contain anything that might need to be encoded as an <a href="http://en.wikipedia.org/wiki/XML_entity" rel="noreferrer">entity</a> (like the less-than which often needs to be output as <strong>&amp;lt;</strong>); are you doing it correctly?</p>\n\n<ul>\n<li>Second, maintainability: is the code that builds the XML easy to understand <em>by someone else</em>?</li>\n</ul>\n\n<p>You probably don\'t want to be stuck with the code for life. I\'ve worked with second-hand C++ code that hand-builds XML and it can be surprisingly obscure. Of course, if this is a personal project of yours, then you don\'t need to worry about "others": substitute "in a year" for "others" above.</p>\n\n<p>I wouldn\'t worry about performance. If your XML is simple enough that you can hand-write it, any overhead from the library is probably meaningless. Of course, your case might be different, but you should measure to prove it first.</p>\n\n<p>Finally, Yes; you can hand build XML text by hand if it\'s simple enough; but not knowing the libraries available is <em>probably</em> not the right reason.</p>\n\n<p>A modern XML library is a quite powerful tool, but it can also be daunting. However, learning the essentials of your XML library is not that hard, and it can be quite handy; among other things, it\'s almost a requisite in today\'s job marketplace. Just don\'t get bogged down by namespaces, schemas and other fancier features until you get the essentials.</p>\n\n<p>Good luck.</p>\n'}, {'answer_id': 24955, 'author': 'SerialSeb', 'author_id': 2593, 'author_profile': 'https://Stackoverflow.com/users/2593', 'pm_score': 3, 'selected': False, 'text': "<p>Xml is hard. Parsing yourself is a bad idea, it's even a worse idea to generate content yourself. Have a look at the Xml 1.1 spec.</p>\n\n<p>You have to deal with such things as proper encoding, attribute encoding (e.g.,  produces invalid xml), proper CDATA escaping, UTF encoding, custom DTD entities, and that's without throwing in the mix xml namespaces with the default / empty namespace, namespace attributes, etc.</p>\n\n<p>Learn a toolkit, there's plenty available.</p>\n"}, {'answer_id': 25187, 'author': 'Sören Kuklau', 'author_id': 1600, 'author_profile': 'https://Stackoverflow.com/users/1600', 'pm_score': 2, 'selected': False, 'text': "<p>By not using a library, you risk generating or parsing data that isn't well-formed, which sooner or later <em>will</em> happen. For the same reason <code>document.write</code> isn't allowed in XHTML, you shouldn't write your XML markup as a string.</p>\n"}, {'answer_id': 25191, 'author': 'Michał Piaskowski', 'author_id': 1534, 'author_profile': 'https://Stackoverflow.com/users/1534', 'pm_score': 1, 'selected': False, 'text': '<p>You don\'t have to use library to parse XML, but check out this question <br><a href="https://stackoverflow.com/questions/18428/what-considerations-should-be-made-before-reinventing-the-wheel">What considerations should be made before reinventing the wheel?</a><br>\nbefore you start writing your own code for parsing/generating xml.</p>\n'}, {'answer_id': 27706, 'author': 'Matt Large', 'author_id': 2978, 'author_profile': 'https://Stackoverflow.com/users/2978', 'pm_score': 1, 'selected': False, 'text': "<p>The only time I've done something like this in production code was when a collegue and I built a pre-processor so that we could embed XML fragments from other files into a larger XML. On load we would first parse these embed (file references in XML comment strings) and replace them with the actual fragment they referenced. Then we would pass on the combined result to the XML Parser.</p>\n"}, {'answer_id': 733048, 'author': 'StaxMan', 'author_id': 59501, 'author_profile': 'https://Stackoverflow.com/users/59501', 'pm_score': 2, 'selected': False, 'text': '<p>Yes.</p>\n\n<p>It makes no sense to skip essential tool: even writing xml is non-trivial with having to escape those ampersands and lts, not to mention namespace bindings (if needed).\nAnd in the end libs can generally read and write xml not only more reliably but more efficiently (esp. so for Java).</p>\n\n<p>But you may have been looking at wrong tools, if they seem overcomplicated. Data binding using JAXB or XStream is simple; but for simple straight-forward XML output, I go with <a href="http://staxmate.codehaus.org/Tutorial" rel="nofollow noreferrer">StaxMate</a>. It can actually simplify the task in many ways (automatically closes start tags, writes namespace declarations if needde etc).</p>\n'}]
['https://Stackoverflow.com/questions/24866', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/184/']