Solved

XML Element Value

Posted on 2006-11-07
10
439 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
ID: 17892046
You need to implement characters - why did you comment it out?
0
 

Author Comment

by:lasutton
ID: 17892066
Does this have any affect on what I need?
0
 

Author Comment

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

Expert Comment

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

Author Comment

by:lasutton
ID: 17892106
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 17892143
>>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
ID: 17892196
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
ID: 17892456
This shows you what you need to know:

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

Expert Comment

by:CEHJ
ID: 17893102
:-)
0
 

Author Comment

by:lasutton
ID: 17893172
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Impossible to extract MSI from new JAVA releases 2 47
compre toata in where clue oracle 4 55
diffSum example 4 20
Download a website to hdd 2 48
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

932 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

14 Experts available now in Live!

Get 1:1 Help Now