?
Solved

Read XML-data into a JSP-array

Posted on 2005-05-03
2
Medium Priority
?
347 Views
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>
      <record type="person">
            <item type="name">Suzy</item>
            <item type="age">31</item>
            <item type="gender">F</item>
      </record>
</data>

// */
%>
0
Comment
Question by:grexx
  • 2
2 Comments
 
LVL 29

Accepted Solution

by:
bloodredsun earned 1000 total points
ID: 13918366
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 = "" ;
            try{
                  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" ;
                  }else{
                        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);
                        text.append(getText(child));
                  }
            }
            return text.toString();
      
      }
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 14112633
Thanks grexx :-)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Most folks would know the basics of how Dropbox works, so that’s not the purpose of this article. Security is what it’s all about, so here I’ll share how I choose to secure my Dropbox Account and the Data it contains.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

862 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