Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XML Element Value

Posted on 2006-11-07
10
Medium Priority
?
460 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 375 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
Industry Leaders: 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!

 
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
 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

715 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