-3

In my table i'm maintains one xml column in that column data in xml format,so now i got this column with query.

Now how to read xml format data.

here my code:

public Object readingSqlResultedRecord(ResultSet result)
{

    try {

        String xml = result.getString(1);
        System.out.println("----xml----"+xml);
    }catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

S.o.p printing xml data,in that data contains one property tag,that tag contains id and name and title. Now how to get title attribute.

Sri
  • 179
  • 3
  • 9
  • 19
  • you bneed to parse an xml string.use dom parser into order to parse xml and get the attribute that you want using Xpath class in dom parser. – Musaddique S Feb 05 '16 at 11:51
  • This question shows no research effort. A Google search would have easily told you the answer. – dryairship Feb 05 '16 at 11:59

2 Answers2

0

You can parse XML using java DOM as follow;

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(yourXmlfromTable));
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(is);

        doc.getDocumentElement().normalize();
        NodeList nList = doc.getElementsByTagName("header");;

       for (int temp = 0; temp < nList.getLength(); temp++) {
            Node node = nList.item(temp);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
             String id= element.getAttribute("id");
             System.out.println(id);
             String name =  element.getAttribute("name");
             System.out.println(name);
             String title =  element.getAttribute("title");
             System.out.println(title);
         }
    }
Vaseph
  • 704
  • 1
  • 8
  • 20
0

You need to parse the string as xml. This has been asked before in stackoverflow:

How to parse a String containing XML in Java and retrieve the value of the root node?

java convert string to xml and parse node

Community
  • 1
  • 1