Several packaging methods for SAX parsing XML

xiaoxiao2021-03-13  177

Oh, it is convenient to find from the PetStore code.

Get root node

Public Element loadDocument (url url) {document doc = null; try {infutsource Xmlinp = new inputsource (url.openstream ());

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance (); DocumentBuilder parser = docBuilderFactory.newDocumentBuilder (); doc = parser.parse (xmlInp); Element root = doc.getDocumentElement (); root.normalize (); return root;} catch (SAXParseException err ) {System.out.println ("Signondao ** Parsing Error" " Err.GetLinenumber () ", URI " Err.GetsystemID ()); System.out.Println (" Signondao Error: " Err.getMessage ());} catch (SAXEXCEPTION E) {system.out.println ("Signondao Error:" E);} catch (java.net.malformedurlexception mfx) {system.out.println ("Signondao Error : " MFX);} catch (java.io.ioException e) {system.out.println (" Signondao Error: " E);} catch (exception pce) {system.out.println (" Signondao error: " PCE);} return null;}

2. Several methods for getting child node values ​​and properties:

/ / Get the properties of a child node

public String getSubTagAttribute (Element root, String tagName, String subTagName, String attribute) {String returnString = ""; NodeList list = root.getElementsByTagName (tagName); for (int loop = 0; loop

public String getSubTagValue (Node node, String subTagName) {String returnString = ""; if (! node = null) {NodeList children = node.getChildNodes (); for (int innerLoop = 0; innerLoop

public String getTagValue (Element root, String tagName) {String returnString = ""; NodeList list = root.getElementsByTagName (tagName); for (int loop = 0; loop

转载请注明原文地址:https://www.9cbs.com/read-129067.html

New Post(0)