XML解析工具类

    xiaoxiao2021-03-30  35

    这是一个接收xml格式的string,并进行解析的工具类。 注:该工具类参考了其他网友的代码,链接忘了。 package com.jackie.mytestproject.xmltest; import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; 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; /** * @author jackie * */ public class XMLBuilder { /** xml string */ private String xmlStr=null; /** xml对应的document */ private Document doc=null; /** xml根结点 */ private Element root=null; /** *构造函数说明: <p> *参数说明:@param path <p> * @throws IOException * @throws SAXException * @throws ParserConfigurationException **/ public XMLBuilder(String xmlStr) throws ParserConfigurationException, SAXException, IOException { this.xmlStr=xmlStr; doc = getDocument(xmlStr); buildRoot(); } public Document getDocument(String xml) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); StringReader sr = new StringReader(xml); InputSource is = new InputSource(sr); Document doc = builder.parse(is); return doc; } /** * 方法名称:buildRoot<p> * 方法功能:生成XML的根结点<p> * 参数说明: <p> * 返回:void <p> **/ private void buildRoot() { root=doc.getDocumentElement(); } public NodeList getNodeList() { return root.getChildNodes(); } public Element [] getElementsByName(String name) { List<Node> resList=new ArrayList<Node>(); NodeList nl=getNodeList(); for(int i=0;i<nl.getLength();i++) { Node nd=nl.item(i); if(nd.getNodeName().equals(name)) { resList.add(nd); } } Element [] res=new Element [resList.size()]; for(int i=0;i<resList.size();i++) { res[0]=(Element)resList.get(i); } return res; } public static String getElementName(Element element) { return element.getNodeName(); } public String getElementValue(Element element) { NodeList nl=element.getChildNodes(); for(int i=0;i<nl.getLength();i++) { if(nl.item(i).getNodeType()==Node.TEXT_NODE)//是一个Text Node { return element.getFirstChild().getNodeValue(); } } return null; } public String getElementAttr(Element element,String attr) { return element.getAttribute(attr); } /** * @return 返回 doc。 */ public Document getDoc() { return doc; } /** * @param doc 要设置的 doc。 */ public void setDoc(Document doc) { this.doc = doc; } /** * @return 返回 xmlStr。 */ public String getXmlStr() { return xmlStr; } /** * @param path 要设置的 xmlStr。 */ public void setXmlStr(String xmlStr) { this.xmlStr = xmlStr; } /** * @return 返回 root。 */ public Element getRoot() { return root; } /** * @param root 要设置的 root。 */ public void setRoot(Element root) { this.root = root; } } 测试类: package com.jackie.mytestproject.xmltest; import org.w3c.dom.Element; public class XMLBuilderTester { public static void main(String[] args) { try { StringBuffer sb = new StringBuffer(); sb.append("<MSG type=\"msg\" sequence=\"\" calltype=\"\">"); sb.append("<Result code=\"0\" attr=\"test\" attr2=\"\" attr3=\"\" attr4=\"\"/>"); sb.append("</MSG>"); String xml = sb.toString(); XMLBuilder xmlBl = new XMLBuilder(xml); Element[] eList = xmlBl.getElementsByName("Result"); System.out.println(xmlBl.getElementValue(eList[0])); System.out.println(xmlBl.getElementAttr(eList[0], "attr")); } catch(Exception e) { e.printStackTrace(); } } }
    转载请注明原文地址: https://ju.6miu.com/read-665209.html

    最新回复(0)