Solved

XML Element Value

Posted on 2006-11-07
10
452 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 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

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!

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

628 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