Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 465
  • Last Modified:

XML Element Value

I'm trying to run through a document using SAX and have to use siimply what's available in 1.4.x of the JDK (no outside JARs).

Below is an example of my handler.  I need to extract the element value, not attribute value and need some guidance on how to do that.

import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.*;

class ProcessMyXMLHandler extends DefaultHandler
{

      public void startDocument(){
            System.out.println("Parsing stuff.xml");
      }
      
      public void startElement(String uri,String localName,String qualName,Attributes attribs)
      {
            System.out.print("Start tag: ");
            System.out.println(qualName);
            System.out.println(attribs.getValue("theElementIamLookingFor"));            
            for(int i=0;i<attribs.getLength();i++)
            {
                                                // this is fine for the attributes, but I want the value of the element.
                  System.out.print("Attributes: ");
                  System.out.print(attribs.getQName(i));
                  System.out.print(" = ");
                  System.out.println(attribs.getValue(i));
            }
      }
      
      public void characters
            (char[] charArray, int start, int length)
      {
            /*
            String charString = new String(charArray, start, length);
            charString = charString.replaceAll("\n","[cr]");
            charString = charString.replaceAll(" ","[blank]");
            System.out.print(length + " characters: ");
            System.out.println(charString);
            */
      }

      public void endElement(String uri,String localName,String qualName,Attributes attribs)
      {
            System.out.print("End tag: ");
            System.out.println(qualName);
      }
      
      public void endDocument()
      {
            System.out.println("Finished Parsing xml file");
      }
}      
0
lasutton
Asked:
lasutton
  • 5
  • 5
1 Solution
 
CEHJCommented:
You need to implement characters - why did you comment it out?
0
 
lasuttonAuthor Commented:
Does this have any affect on what I need?
0
 
lasuttonAuthor Commented:
Ok, I see that the value is displayed in the "characters" method, how is this properly accessed?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
Very much so - that's how you get element content (text nodes)
0
 
lasuttonAuthor Commented:
Can you tell me what I need to do in the sample code to get the element value then?  I am not sure how the characters method is tied into the name of the element itself.
0
 
CEHJCommented:
>>I am not sure how the characters method is tied into the name of the element itself.

It isn't tied in at all. You need to save the current node name as an instance variable and check its value and you'll have to collect characters whether it's time to or not. You'll determine whether it's time by checking the current node.

If you want a more context-sensitive approach., use an XML pull parser
0
 
lasuttonAuthor Commented:
That went completely over my head :)

Can you provide a small example? (not looking for complete code, just how to implement).

In regards to the parser, I want to make sure I only use the internal parsers and no other JARs are required to run the application.


0
 
CEHJCommented:
This shows you what you need to know:

http://www.saxproject.org/quickstart.html
0
 
CEHJCommented:
:-)
0
 
lasuttonAuthor Commented:
Basically once you pointed out the characters method was required it pointed me in the right direction.  I basically created 2 static variables in the class for the name and values.  Update the name in the endElement and the value in the characters and it seems to work well.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now