Advertisement

02.26.2004 at 02:24PM PST, ID: 20899490
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

Parsing the XML Doc

Asked by ashish_xema in Extensible Markup Language (XML)

Hi,

I am using the following code to parse a XML doc.

******

import java.io.*;

import org.xml.sax.*;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;

public class BooksLibrary extends HandlerBase
{
    protected static final String XML_FILE_NAME = "E:\\Properties File\\Sample.xml";

    public static void main (String argv [])
    {
        // Use the default (non-validating) parser
       // SAXParserFactory factory = SAXParserFactory.newInstance();
            //factory.setValidating(true);

        SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
            // Set up output stream
            out = new OutputStreamWriter (System.out, "UTF8");

            // Parse the input
            SAXParser saxParser = factory.newSAXParser();
            saxParser.parse( new File(XML_FILE_NAME), new BooksLibrary() );

        } catch (Throwable t) {
            t.printStackTrace ();
        }
        System.exit (0);
    }

    static private Writer  out;

    //===========================================================
    // Methods in SAX DocumentHandler
    //===========================================================

    public void startDocument ()
    throws SAXException
    {
        showData ("<?xml version='1.0' encoding='UTF-8'?>");
        newLine();
    }

    public void endDocument ()
    throws SAXException
    {
        try {
            newLine();
            out.flush ();
        } catch (IOException e) {
            throw new SAXException ("I/O error", e);
        }
    }

    public void startElement (String name, AttributeList attrs)
    throws SAXException
    {
        showData ("<"+name);
        if (attrs != null) {
            for (int i = 0; i < attrs.getLength (); i++) {
                showData (" ");
                showData (attrs.getName(i)+"=\""+attrs.getValue (i)+"\"");
            }
        }
        showData (">");
    }

    public void endElement (String name)
    throws SAXException
    {
        showData ("</"+name+">");
    }

    public void characters (char buf [], int offset, int len)
    throws SAXException
    {
        String s = new String(buf, offset, len);
        showData (s);
    }

    //===========================================================
    // Helpers Methods
    //===========================================================

    // Wrap I/O exceptions in SAX exceptions, to
    // suit handler signature requirements
    private void showData (String s)
    throws SAXException
    {
        try {
            out.write (s);
            out.flush ();
        } catch (IOException e) {
            throw new SAXException ("I/O error", e);
        }
    }

    // Start a new line
    private void newLine ()
    throws SAXException
    {
        String lineEnd =  System.getProperty("line.separator");
        try {
            out.write (lineEnd);
        } catch (IOException e) {
            throw new SAXException ("I/O error", e);
        }
    }




}

*******

My XML file look like this

******

<?xml version="1.0" encoding="UTF-8"?>

<Body>
    <Response>
      <ActCd>1000</ActCd>
      <RtrnCd>123</RtrnCd>
      <GlobalUserId>123213</GlobalUserId>
      <FirstNm>John</FirstNm>
      <LastNm>Manathon</LastNm>
      <MiddleNm>Willy</MiddleNm>
      <ElectronicAddrTxt>Something here</ElectronicAddrTxt>
      <Userid>test</Userid>
      <TransAcctNbr>234234234324</TransAcctNbr>
    </Response>
  </Body>

******
This code is returning me  this same XML. I need the out put in different form:

I need the out in the following form:

ActCd :- 1000
FirstNm: John
----
----
---

Like this all the elements of the XML. Do you have any idea what needs to be changed to get this output.

Thanks in advance

XMEAStart Free Trial
 
Loading Advertisement...
 
[+][-]02.26.2004 at 09:02PM PST, ID: 10466740

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Extensible Markup Language (XML)
Sign Up Now!
Solution Provided By: exile_4
Participating Experts: 1
Solution Grade: A
 
 
[+][-]11.15.2004 at 05:33AM PST, ID: 12583544

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]11.19.2004 at 02:47AM PST, ID: 12623605

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32