Java xml notes

Submitted by code_admin on Fri, 07/20/2018 - 16:17

General

Look at my general utilities in metcarob.com.personalservices.library.db.XMLUtilities
This includes saving XML document to string

Simple read XML data

  1.         String xmlString = "" +
  2.                            "<test xmlns='abc'>" +
  3.         "<item>1</item>" +
  4.         "<item>2</item>" +
  5.         "<item>3</item>" +
  6.                            "</test>";
  7.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  8.         dbf.setNamespaceAware(true);
  9.         Document doc;
  10.         try {
  11.             doc = dbf
  12.                .newDocumentBuilder()
  13.                .parse(new ByteArrayInputStream(xmlString.getBytes()));
  14.             Element node = doc.getDocumentElement();
  15.             System.out.println("Doc element:"+node.getTagName());
  16.             System.out.println("Doc element content:"+node.getTextContent());
  17.            
  18.             NodeList nl;
  19.             nl = node.getElementsByTagNameNS("abc","item");
  20.             System.out.println("Found " + nl.getLength() + " items");
  21.             for(int c=0;c<nl.getLength();c++) {
  22.                 System.out.println(nl.item(c).getTextContent());
  23.             }
  24.         } catch (ParserConfigurationException e) {
  25.             System.out.println(e.toString());
  26.         } catch (SAXException e) {
  27.             System.out.println(e.toString());
  28.         } catch (IOException e) {
  29.             System.out.println(e.toString());
  30.         }

Tags

RJM Article Type
Work Notes