Just want to simply generate an XML string, found that the appropriate solution, so from the foreign C language generated XML tools translated one over. Original link
package nc.vo.yceai.pub;import java.util.stack;import java.util.vector;/** * Target: Returns an XML text * reason: is too cumbersome to piece together, dom too heavyweight, provides an intermediate build-XML tool * * @ version v5.0 * @author wuzq * @date 2014-11-6 * @see http://www.codeproject.com/articles/13498/xmlwriter-a-simple-reusable-class */public class stringxmlwriter { private stringbuilder m_ xml;//output private int m_ilevel;//to handle indents private vector<string> m_attrvect;//Store Properties, only the properties of the current label, the output label and clear private stack <string> m_tagstack;//Store label, for automatic output off label public static void main (String[] args) { // example stringxmlwriter billxml = new stringxmlwriter (); billxml.addcomment ("Example"); billxml.addattributes ("attr", "ROOT");//, You can add any property before adding a label billxml.createtag ("Bills");//Add Tags Billxml.addattributes ("attr", "Documents"); billxml.createtag (" Bill "); Billxml.addcomment ("table header"); billxml.createtexttag ("Pk_corp"), "1001"); billxml.createtexttag ("Pk_billtype", "YXB8" ); Billxml.addcomment ("Table body"); &nbsP; billxml.createtag ("Bodys"); billxml.addattributes ("attr", "first line"); billxml.createtag ("Body"); Billxml.createtexttag ("Pk_corp", "1001"); Billxml.closelasttag (); billxml.addattributes ("attr", "second row"); Billxml.createtag ("Body"); billxml.createtexttag ("Pk_corp", "1002"); billxml.closelasttag (); billxml.closealltags (); system.out.println (billxml); &nbsP; } public stringxmlwriter () { super (); m_xml = new stringbuilder (); m_ Attrvect = new vector<string> (); m_ Tagstack = new stack<string> (); string encoding = null; try{ encoding = yceaipubconfig.getinstance (). GetEncode (); }catch (exception e) { encoding = "Utf-8"; } m_xml.append ("<?xml version=\" 1.0\ " encoding=\" " + encoding + "\"?> "); } /** * Add XML tag, if the label has a property, you must first add the property * @param tag * @author wuzq * @ date 2014-11-6 pm 3:33:48 */ public void createtag (String tag) { m_xml.append ("\ n"); for (int i =0;i<m_ilevel;i++) m_xml.append ("\ t"); m_xml.append ("<"). Append (tag); addattrtotag (); m_xml.append (">"); m_tagstack.push (tag); m_ilevel++; } private void addattrtotag () { while (0 < m_ Attrvect.size ()/2) { string attr = m_attrvect.remove (M_attrvect.size ()-1); m_xml.append (" "). Append (attr). Append ("="); attr = m_attrvect.remove (M_attrvect.size ()-1); m_xml.append ("\" "). Append (attr). Append (" \ ""); } m_attrvect.clear (); } /** * Closing Labels * * @author wuzq * @date 2014-11-6 Afternoon 3:38:28 */ public void Closelasttag () { m_xml.append ("\ n"); m_ilevel--; for (int i=0; i <m_ilevel; i++) { m_xml.append (" \ t "); } m_ Xml.append ("</"). Append (M_tagstack.pop ()). Append (">"); } /** * Close all Tabs * * @author wuzq * @date 2014-11-6 pm 3:49:44 */ public void closealltags () { while (M_tagstack.size () != 0) { m_xml.append ("\ n"); m_ilevel--; for (int i= 0;i<m_ilevel; i++) { m_xml.append ("\ t"); } m_xml.append ("</"). Append (m_ Tagstack.pop ()). Append (">"); } } /** * Add a child label, if the child tag has a property, you must first add the attribute * @param tag * @param value * @author wuzq * @date 2014-11-6 pm 3:50:37 */ public void createtexttag (String tag, String value) { m_xml.append ("\ n"); for (int i=0; i<m_ilevel; i++) { m_xml.append ("\ t"); } m_xml.append ("<"). Append (tag); addattrtotag (); m_xml.append (">"). Append ( Value). Append ("</"). Append(tag). Append (">"); } /** * Add Properties * @param key * @param value * @author wuzq * @date 2014-11-6 pm 3:56:04 */ public void addattributes (String key, string value) { m_attrvect.addelement (value); m_attrvect.addelement (key); } /** * Add comments * @param comment * @author wuzq * @date 2014-11-6 pm 3:58:48 */ &nBsp; public void addcomment (string comment) { m_xml.append ("\ n"); for (int i=0; i<m_ilevel; i++) { m_xml.append ("\ t"); } m_xml.append ("<! --"). Append (comment). Append ("--"); } @Override public string tostring () { return m_xml.tostring (); }}
The result of the sample execution:
<?xml version= "1.0" encoding= "Utf-8"?><!--example--><bills attr= "ROOT" > <bill attr= "Documents" > <!--headers-- > <pk_corp>1001</pk_corp> <pk_billtype>YXB8</pk_billtype> <!--Watch Body--> <bodys> <body attr= "First line" > <pk_corp>1001</pk_corp> </Body> <body attr= "Second row" > <pk_corp>1002</pk_corp> </Body> </Bodys> </Bill></Bills>
Java Generation XML Lightweight Tool