Solved

XML Element Value

Posted on 2006-11-07
10
438 Views
Last Modified: 2013-11-19
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
Comment
Question by:lasutton
  • 5
  • 5
10 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
Comment Utility
You need to implement characters - why did you comment it out?
0
 

Author Comment

by:lasutton
Comment Utility
Does this have any affect on what I need?
0
 

Author Comment

by:lasutton
Comment Utility
Ok, I see that the value is displayed in the "characters" method, how is this properly accessed?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Very much so - that's how you get element content (text nodes)
0
 

Author Comment

by:lasutton
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
 

Author Comment

by:lasutton
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
This shows you what you need to know:

http://www.saxproject.org/quickstart.html
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0
 

Author Comment

by:lasutton
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maven java path setting 5 49
Eclipse Neon and jdk 1.8.0 11 63
java  and programming certification ? 4 62
Stupid git question 2 17
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn how to dynamically set the form action using jQuery.

763 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

6 Experts available now in Live!

Get 1:1 Help Now