Read XML-data into a JSP-array

Posted on 2005-05-03
Last Modified: 2008-01-09
I would like to be able to read xml-data into a jsp-array. Take the following example. The array xmltest now has 2 x 3 items for two persons. I've created a resembling structure in xml. The tag-names and xml-structure may be different, that's not the issue here. What I need is some function that reads trough the xml data. The xml can be in an include if that's easier.

<% String[] persons =
      "Simon", "35", "M",
      "Suzy", "31", "F"
}; // name, age, gender


<data type="persons">
      <record type="person">
            <item type="name">Simon</item>
            <item type="age">35</item>
            <item type="gender">M</item>
      <record type="person">
            <item type="name">Suzy</item>
            <item type="age">31</item>
            <item type="gender">F</item>

// */
Question by:grexx
    LVL 29

    Accepted Solution

    Here's a couple of methods to help you:

           * this method retrieves the value of the specifed node in the xml String
           * pStr
           * @param pStr
           * @return
          public static String getNamedNodeValue( String pStr , String pNodename  ){
                String jobNumber = "" ;
                      pStr = pStr.trim() ;
                      DocumentBuilderFactory factoryBuilder = DocumentBuilderFactory.newInstance( );
                      DocumentBuilder builder = factoryBuilder.newDocumentBuilder();
                      Document doc = builder.parse( new ByteArrayInputStream( pStr.getBytes()) );
                      //Get root element
                      Element elDoc = doc.getDocumentElement();
                      NodeList nodes = elDoc.getElementsByTagName( pNodename );
                      if ( nodes.getLength() != 1 ){
                            return "error" ;
                            Node jobNode = nodes.item(0) ;
                            jobNumber = getText( jobNode );
                catch(Exception e){
                      e.printStackTrace() ;
                return jobNumber ;

          /**private method to return the text data between the opening and closing
           * tags of the specific nodes when applied to an org.w3c.dom.Document object
           * @param node
           * @return
          private static String getText(Node node) {
                StringBuffer text = new StringBuffer();
                String value = node.getNodeValue();
                if (value != null) text.append(value);
                if (node.hasChildNodes()) {
                      NodeList children = node.getChildNodes();
                      for (int i = 0; i < children.getLength(); i++) {
                            Node child = children.item(i);
                return text.toString();
    LVL 29

    Expert Comment

    Thanks grexx :-)

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    This post first appeared at Oracleinaction  ( Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
    For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now