Jdom, Dom4j, W3c, String mutual conversion Daquan and take Xml attribute values, set Xml attribute values, delete Xm attribute values source code: http: www.zuidaima.com%1550463324146688.htm
Conversion between Jdom, Dom4j, W3c, and String, obtaining Xml attribute values, setting Xml attribute values, and deleting Xm attribute values
Source code:
Original article: mutual conversion between Jdom, Dom4j, W3c, and String, retrieving Xml attribute values, setting Xml attribute values, and deleting Xm attribute values
Source code: http://www.zuidaima.com/share/1550463324146688.htm
Package com. zuidaima. xml; import java. io. charArrayReader; import java. io. IOException; import java. io. stringReader; import java. io. stringWriter; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import org. apache. xerces. dom. entimpl; import org. apache. xerces. parsers. DOMParser; import org. apache. xml. serialize. outputFormat; import org. apache. xml. serialize. XMLSerializ Er; import org. apache. xpath. XPathAPI; import org. jdom. input. DOMBuilder; import org. w3c. dom. document; import org. w3c. dom. element; import org. w3c. dom. node; import org. w3c. dom. nodeList; import org. xml. sax. inputSource; import org. xml. sax. SAXException;/*** convert the document Object to string data * @ author www.zuidaima.com * @ param dom * @ return */public class XmlChange {/*** to convert the document object, convert to string data * @ param dom * @ return */public Static String dom2String (Document dom) // converts a document object to String data. {String aa = new String (); try {StringWriter ss = new StringWriter (); OutputFormat format = new OutputFormat (dom); // Serialize DOMformat. setEncoding ("GB2312"); XMLSerializer serial = new XMLSerializer (ss, format); serial. asDOMSerializer (); // As a DOM Serializerserial. serialize (dom. getDocumentElement (); aa = ss. toString (); ss. flush (); ss. close ();} catch (Exception e) {// return false;} return aa ;}/*** String to document * @ author www.zuidaima.com * @ param XMLData * @ return */public static Document string2Dom (String XMLData) // Parse String xml data and generate document {// System. out. println ("xml:" + XMLData); Document dom = new jsonentimpl (); try {InputSource source = new InputSource (new CharArrayReader (XMLData. toCharArray (); System. out. println ("----- 888888 =" + source); DocumentBuilderFactory docBuilderFactory = DocumentBu IlderFactory. newInstance (); DocumentBuilder docBuilder = docBuilderFactory. newDocumentBuilder (); dom = docBuilder. parse (source); System. out. println ("----- 888888 =" + dom);} catch (Exception e) {dom = null;} finally {return dom ;}/ *** parse string xml data, generate a document and filter the spaces in the xml. * @ author www.zuidaima.com * @ param data * @ param isSpace * @ return * @ throws IOException * @ throws SAXException */public static escape E Nt string2Dom (String data, boolean isSpace) throws IOException, SAXException // Parse the String xml data, generate a document, filter spaces in the xml {if (isSpace) {DOMParser parser = new DOMParser (); StringReader sr = new StringReader (data); InputSource is = new InputSource (sr); parser. parse (is); sr. close (); return parser. getDocument () ;}else {return null ;}/ *** implements dom4j to org. w3c. dom. document Conversion ** @ param doc * @ return * @ throws Exception * /Public static org. w3c. dom. document dom4j2W3c (Document doc) throws Exception {if (doc = null) {return (null);} java. io. stringReader reader = new java. io. stringReader (doc. toString (); org. xml. sax. inputSource source = new org. xml. sax. inputSource (reader); javax. xml. parsers. documentBuilderFactory documentBuilderFactory = javax. xml. parsers. documentBuilderFactory. newInstance (); javax. xml. parsers. documentBu Ilder documentBuilder = documentBuilderFactory. newDocumentBuilder (); return (documentBuilder. parse (source);}/***** implement org. w3c. dom. conversion from Document to dom4j ** @ param doc * @ return * @ throws Exception */public static org. dom4j. document parse (org. w3c. dom. document doc) throws Exception {if (doc = null) {return (null);} org. dom4j. io. DOMReader xmlReader = new org. dom4j. io. DOMReader (); return (xmlReader. read (do C);}/***** implement org. w3c. dom. conversion from Document to jdom ** @ param doc * @ return * @ throws Exception */public static org. jdom. document convertToJDOM (org. w3c. dom. document doc) throws Exception {if (doc = null) {return (null);} DOMBuilder builder = new DOMBuilder (); org. jdom. document jdomDoc = builder. build (doc); return jdomDoc;}/*** according to the tagName, attributeName obtains the corresponding Node attribute value from xml and may have multiple nodes defined by tagName, take the first ** @ param d Om * @ param tagName * @ attributeName * @ return Attribute; */public static String getAttributeValueFromDom (Document dom, String tagName, String attributeName) {String result = ""; if (dom! = Null & tagName! = Null &&! TagName. trim (). equals ("") & attributeName! = Null &&! AttributeName. trim (). equals ("") {NodeList nodeList = null; Node node = null; nodeList = dom. getElementsByTagName (tagName. trim (); if (nodeList! = Null & nodeList. getLength ()> 0) {/*** there may be multiple nodes defined by tagnames in xml, and the first Node is */node = nodeList. item (0); if (node! = Null) {result = (Element) node). getAttribute (attributeName); result = null? "": Result. trim () ;}} return result ;} /*** set the xml node property value ** @ param dom * @ param tagName * @ param attributeName * @ param value * @ return */public static boolean setAttributeValueFromDom (Document dom, string tagName, String attributeName, String value) {boolean result = false; if (dom! = Null & tagName! = Null &&! TagName. trim (). equals ("") & attributeName! = Null &&! AttributeName. trim (). equals ("") {NodeList nodeList = null; Node node = null; nodeList = dom. getElementsByTagName (tagName. trim (); if (nodeList! = Null & nodeList. getLength ()> 0) {/*** there may be multiple nodes defined by tagnames in xml, and the first Node is */node = nodeList. item (0); if (node! = Null) {(Element) node ). setAttribute (attributeName, value); result = true ;}} return result ;} /*** get the corresponding Node value from xml based on the tagName ** @ param dom * @ param tagName * @ return */public static String getNodeValueFromDom (Document dom, String tagName) {String result = ""; try {if (dom! = Null & tagName! = Null &&! TagName. trim (). equals ("") {NodeList nodeList = null; Node node = null; nodeList = XPathAPI. selectNodeList (dom, tagName. trim (); // dom. getElementsByTagName (tagName. trim (); if (nodeList! = Null & nodeList. getLength ()> 0) {/*** there may be multiple nodes defined by tagnames in xml, and the first Node is */node = nodeList. item (0); if (node! = Null) {node = node. getFirstChild (); if (node! = Null) result = node. getNodeValue (); result = null? "": Result. trim () ;}}} catch (Exception e) {System. out. println ("getNodeValueFromDom error =" + e. toString (); e. printStackTrace ();} return result;} public static String getTagValueByTagName (Document dom, String tagName) {String result = ""; try {if (dom! = Null & tagName! = Null &&! TagName. trim (). equals ("") {NodeList nodeList = null; Node node = null; if (dom. getElementsByTagName (tagName. trim ())! = Null) {nodeList = dom. getElementsByTagName (tagName. trim (); if (nodeList. item (0 )! = Null) {node = nodeList. item (0); if (node. getChildNodes (). item (0). getNodeValue ()! = Null) {result = (node. getChildNodes ()). item (0 ). getNodeValue (). trim () ;}}}} catch (Exception e) {System. out. println ("getNodeValueFromDom error =" + e. toString (); e. printStackTrace ();} return result ;}}