Help With the object model of Java

Hi,

I need to parse the following

xml files (xml1 and xml2)

I am opening them in the SAX Parser but I am unable to figure out a generalized way of traversing. I want to make sure that I fully traverse the tree in a very general way as the xml nodes can increase and decrease as shown in the two files.

My Trial code for Java is attached in CodeForPrintingtheparsedXML.txt


I have been successful in doing this in PHP by the following code. I hope a similar object model exists in Java. My PHP Code is below


_______________________________________________________________________


// A DELIMITER
$dlm = '|';

// MAKE AN OUTPUT BUFFER
$outattrib = NULL;
$outmetric = NULL;
$outreportdata=NULL;


// MAKE AN OBJECT
$obj = SimpleXML_Load_String($reportxmldata);


foreach ($obj->report_data_list->report_data->objects->attribute as $object)
{
    $outattrib
    .= (string)$object["name"].$dlm.PHP_EOL;
}


foreach ($obj->report_data_list->report_data->objects->metric as $object)
{
    $outmetric
    .= (string)$object["name"].$dlm.PHP_EOL;
}



$outreportdata.="<br/>";

foreach ($obj->report_data_list->report_data->raw_data->rows->r as $row)
{
   
   
   foreach ($row->v as $robj)
    {
           
        $outreportdata.= (string)$robj.$dlm.PHP_EOL;            
         
             
    }
   
   $outreportdata.="<br/>";
   
}


// SHOW All the Attributes
$outattrib = trim($outattrib);
print_r($outattrib);


// SHOW All the Metrics
$outmetric = trim($outmetric);
print_r($outmetric);




// SHOW All the report_data
$outreportdata = trim($outreportdata);
print_r($outreportdata);

_______________________________________________________________________
xml1.txt
xml2.txt
parse-xml1.txt
parse-xml2.txt
CodeForPrintingtheparsedXML.txt
anshumaEngineeringAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
for_yanConnect With a Mentor Commented:
There you go with headers.


import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

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


import java.util.ArrayList;
import java.util.HashMap;


public class ParseingXMLWithSAX {


   HashMap m = new HashMap();
    ArrayList<String> ar = null;
       ArrayList<String> ar1 = null;
    String s;
    String key;

  

    String filename =  "xml2.txt";

    public  void getElementFromXML() {
        try {

               ar = new ArrayList<String>();
                 ar1 = new ArrayList<String>();
              s= "";
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            DefaultHandler handler = new DefaultHandler() {

                boolean length1 = false;

                


                public void startElement(String uri, String localName,
                        String qName, Attributes attributes)
                        throws SAXException {

                    if (qName.toUpperCase().equalsIgnoreCase("r")) {

                    }

                    if (qName.toUpperCase().equalsIgnoreCase("v")) {
                        length1 = true;
                    }

                     if (qName.toUpperCase().equalsIgnoreCase("attribute")) {
                         ar1.add(attributes.getValue("name"));

                    }

                               if (qName.toUpperCase().equalsIgnoreCase("metric")) {
                         ar1.add(attributes.getValue("name"));

                    }




                    }
                public void endElement(String uri, String localName,
                        String qName) throws SAXException {

                    if (qName.toUpperCase().equalsIgnoreCase("v")) {
                        length1 = false;


                    }

                      if (qName.toUpperCase().equalsIgnoreCase("r")) {
                           //  System.out.println("");
                          ar.add(s);
                          s="";

                      }



                }
                public void characters(char ch[], int start, int length)
                        throws SAXException {



                   if (length1) {
                       
                     
                        //   System.out.print( new String(ch, start, length) + " |");
                       s+= new String(ch, start, length) + " |";
                    }


                    }


            };
            saxParser.parse(filename, handler);

            for(String s: ar1){
                System.out.print(s + " |");

            }
            System.out.println("");
            for(String s: ar){
                s = s.replace("|& |"," & ");
                System.out.println(s);

            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }





    }

    public static void main(String args[])
    {
        new ParseingXMLWithSAX().getElementFromXML();

    }


}

Open in new window


Output:

Category |Year |Supplier |Units Sold |Revenue |Profit |
Books |2003 |Bantam Books |309 |$4,742 |$1,298 |
John Wiley   &  Sons |271 |$4,726 |$1,320 |
McGraw Hill |114 |$4,382 |$1,182 |
Perigee |334 |$3,015 |$779 |
Prentice Hall |186 |$2,370 |$637 |
Scribner |325 |$3,265 |$780 |
Simon   &  Schuster |214 |$3,218 |$870 |
Vintage Books |287 |$3,321 |$860 |
Warner Books |317 |$4,190 |$1,103 |
2004 |Bantam Books |275 |$4,327 |$1,176 |
John Wiley   &  Sons |206 |$3,468 |$968 |
McGraw Hill |92 |$3,843 |$1,035 |
Perigee |315 |$2,839 |$728 |
Prentice Hall |181 |$2,425 |$650 |
Scribner |311 |$3,139 |$751 |
Simon   &  Schuster |246 |$3,765 |$1,014 |
Vintage Books |278 |$3,234 |$827 |
Warner Books |288 |$3,952 |$1,069 |
Electronics |2003 |ACS Innovations |4,679 |$2,237,510 |$618,668 |
ATF Electronics |577 |$445,700 |$115,648 |
Audiotronics Inc. |2,027 |$407,290 |$109,823 |
Digital Equipment |756 |$64,960 |$17,436 |
Digital Office Inc. |789 |$269,030 |$71,177 |
DSS Appliance Co. |2,247 |$1,101,450 |$305,311 |
Entertaintron Inc. |2,180 |$597,860 |$162,497 |
Impact Components |1,029 |$188,253 |$51,574 |
MegaStore Corp. |819 |$423,213 |$116,456 |
Universal EL |478 |$217,718 |$60,335 |
2004 |ACS Innovations |4,042 |$1,858,920 |$509,858 |
ATF Electronics |598 |$462,500 |$119,981 |
Audiotronics Inc. |1,913 |$380,770 |$102,612 |
Digital Equipment |713 |$58,380 |$15,651 |
Digital Office Inc. |798 |$278,570 |$73,694 |
DSS Appliance Co. |2,086 |$1,036,450 |$287,678 |
Entertaintron Inc. |1,948 |$545,730 |$148,426 |
Impact Components |910 |$161,267 |$44,378 |
MegaStore Corp. |725 |$375,439 |$102,923 |
Universal EL |435 |$190,644 |$52,739 |
Movies |2003 |20th Century Fox |12,841 |$222,671 |$55,661 |
A  & E Entertainment |4,892 |$127,721 |$33,686 |
Columbia Pictures |15,315 |$185,855 |$45,249 |
Disney Studios |9,500 |$160,446 |$40,374 |
Lyons Group |6,414 |$78,758 |$18,691 |
MGM Studios |8,818 |$117,280 |$27,750 |
Paramount Pictures |8,533 |$126,180 |$30,731 |
Sport Vision |966 |$18,993 |$4,878 |
TriStar Pictures |13,716 |$200,425 |$48,996 |
Universal Studios |11,861 |$175,516 |$43,631 |
Ventura Distribution |1,163 |$25,371 |$6,285 |
2004 |20th Century Fox |12,204 |$212,730 |$53,225 |
A  & E Entertainment |4,628 |$120,891 |$31,856 |
Columbia Pictures |14,111 |$171,889 |$41,901 |
Disney Studios |8,727 |$147,355 |$37,042 |
Lyons Group |5,846 |$72,062 |$17,095 |
MGM Studios |7,989 |$106,029 |$25,096 |
Paramount Pictures |7,817 |$115,450 |$28,129 |
Sport Vision |917 |$17,654 |$4,515 |
TriStar Pictures |12,981 |$189,755 |$46,436 |
Universal Studios |10,804 |$160,960 |$40,143 |
Ventura Distribution |1,062 |$24,234 |$6,030 |
Music |2003 |BMG |23,954 |$331,871 |$29,541 |
Columbia House |6,788 |$91,776 |$9,148 |
Polygram |16,608 |$254,615 |$31,212 |
Sony Music |22,574 |$323,576 |$33,592 |
Virgin Records |18,716 |$284,133 |$33,905 |
WEA |11,284 |$165,230 |$22,617 |
2004 |BMG |22,257 |$307,634 |$27,357 |
Columbia House |6,350 |$86,428 |$8,725 |
Polygram |15,447 |$238,817 |$29,461 |
Sony Music |20,696 |$297,124 |$30,800 |
Virgin Records |17,381 |$263,071 |$30,997 |
WEA |10,071 |$148,059 |$20,441 |

Open in new window

0
 
for_yanCommented:
This is the code and output for the first file:

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

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

/*
<product>
 <name>laptop</name>
 <price>120</price>
 <maker>Dell</maker>
</product>

*/
import java.util.ArrayList;
import java.util.HashMap;


public class ParseingXMLWithSAX {


   HashMap m = new HashMap();
    ArrayList ar = null;
    String key;

  

    String filename =  "xml1.txt";

    public  void getElementFromXML() {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            DefaultHandler handler = new DefaultHandler() {
                boolean title = false;
                boolean length1 = false;
                boolean actor = false;
                boolean dvd = false;
                boolean products = false;
                String dvdAttValue;
                String dvdAtt;


                public void startElement(String uri, String localName,
                        String qName, Attributes attributes)
                        throws SAXException {

                    if (qName.toUpperCase().equalsIgnoreCase("r")) {
                        System.out.println("");
                        title = true;
                        ar = new ArrayList();
                    }

                    if (qName.toUpperCase().equalsIgnoreCase("v")) {
                        length1 = true;
                    }




                    }
                public void endElement(String uri, String localName,
                        String qName) throws SAXException {

                    if (qName.toUpperCase().equalsIgnoreCase("v")) {
                        length1 = false;

                    }



                }
                public void characters(char ch[], int start, int length)
                        throws SAXException {



                   if (length1) {
                      // ar.add( new String(ch, start, length));
                           System.out.print( new String(ch, start, length) + " |");
                    }


                    }


            };
            saxParser.parse(filename, handler);

        }
        catch (Exception e) {
            e.printStackTrace();
        }





    }

    public static void main(String args[])
    {
        new ParseingXMLWithSAX().getElementFromXML();

    }


}

Open in new window


Output:

Atlanta |26.92% |
San Diego |27.20% |
San Francisco |26.74% |
Washington, DC |26.40% |
Salt Lake City |27.30% |
Miami |27.03% |
Milwaukee |27.05% |
New Orleans |27.01% |
Seattle |26.73% |
Boston |26.52% |
New York |27.50% |
Fargo |27.22% |
Memphis |26.69% |
Charleston |27.01% |
Web |26.94% |

Open in new window

0
 
for_yanCommented:
This is a little bit cleaned up code (removed stuff from previous program)
and the output from the file xml2.txt (this code woill work of course with the xml1.txt as well)

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

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


import java.util.ArrayList;
import java.util.HashMap;


public class ParseingXMLWithSAX {


   HashMap m = new HashMap();
    ArrayList ar = null;
    String key;

  

    String filename =  "xml2.txt";

    public  void getElementFromXML() {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            DefaultHandler handler = new DefaultHandler() {

                boolean length1 = false;

                


                public void startElement(String uri, String localName,
                        String qName, Attributes attributes)
                        throws SAXException {

                    if (qName.toUpperCase().equalsIgnoreCase("r")) {

                    }

                    if (qName.toUpperCase().equalsIgnoreCase("v")) {
                        length1 = true;
                    }




                    }
                public void endElement(String uri, String localName,
                        String qName) throws SAXException {

                    if (qName.toUpperCase().equalsIgnoreCase("v")) {
                        length1 = false;


                    }

                      if (qName.toUpperCase().equalsIgnoreCase("r")) {
                             System.out.println("");

                      }



                }
                public void characters(char ch[], int start, int length)
                        throws SAXException {



                   if (length1) {
                     
                           System.out.print( new String(ch, start, length) + " |");
                    }


                    }


            };
            saxParser.parse(filename, handler);

        }
        catch (Exception e) {
            e.printStackTrace();
        }





    }

    public static void main(String args[])
    {
        new ParseingXMLWithSAX().getElementFromXML();

    }


}

Open in new window


output:

Books |2003 |Bantam Books |309 |$4,742 |$1,298 |
John Wiley  |& | Sons |271 |$4,726 |$1,320 |
McGraw Hill |114 |$4,382 |$1,182 |
Perigee |334 |$3,015 |$779 |
Prentice Hall |186 |$2,370 |$637 |
Scribner |325 |$3,265 |$780 |
Simon  |& | Schuster |214 |$3,218 |$870 |
Vintage Books |287 |$3,321 |$860 |
Warner Books |317 |$4,190 |$1,103 |
2004 |Bantam Books |275 |$4,327 |$1,176 |
John Wiley  |& | Sons |206 |$3,468 |$968 |
McGraw Hill |92 |$3,843 |$1,035 |
Perigee |315 |$2,839 |$728 |
Prentice Hall |181 |$2,425 |$650 |
Scribner |311 |$3,139 |$751 |
Simon  |& | Schuster |246 |$3,765 |$1,014 |
Vintage Books |278 |$3,234 |$827 |
Warner Books |288 |$3,952 |$1,069 |
Electronics |2003 |ACS Innovations |4,679 |$2,237,510 |$618,668 |
ATF Electronics |577 |$445,700 |$115,648 |
Audiotronics Inc. |2,027 |$407,290 |$109,823 |
Digital Equipment |756 |$64,960 |$17,436 |
Digital Office Inc. |789 |$269,030 |$71,177 |
DSS Appliance Co. |2,247 |$1,101,450 |$305,311 |
Entertaintron Inc. |2,180 |$597,860 |$162,497 |
Impact Components |1,029 |$188,253 |$51,574 |
MegaStore Corp. |819 |$423,213 |$116,456 |
Universal EL |478 |$217,718 |$60,335 |
2004 |ACS Innovations |4,042 |$1,858,920 |$509,858 |
ATF Electronics |598 |$462,500 |$119,981 |
Audiotronics Inc. |1,913 |$380,770 |$102,612 |
Digital Equipment |713 |$58,380 |$15,651 |
Digital Office Inc. |798 |$278,570 |$73,694 |
DSS Appliance Co. |2,086 |$1,036,450 |$287,678 |
Entertaintron Inc. |1,948 |$545,730 |$148,426 |
Impact Components |910 |$161,267 |$44,378 |
MegaStore Corp. |725 |$375,439 |$102,923 |
Universal EL |435 |$190,644 |$52,739 |
Movies |2003 |20th Century Fox |12,841 |$222,671 |$55,661 |
A |& |E Entertainment |4,892 |$127,721 |$33,686 |
Columbia Pictures |15,315 |$185,855 |$45,249 |
Disney Studios |9,500 |$160,446 |$40,374 |
Lyons Group |6,414 |$78,758 |$18,691 |
MGM Studios |8,818 |$117,280 |$27,750 |
Paramount Pictures |8,533 |$126,180 |$30,731 |
Sport Vision |966 |$18,993 |$4,878 |
TriStar Pictures |13,716 |$200,425 |$48,996 |
Universal Studios |11,861 |$175,516 |$43,631 |
Ventura Distribution |1,163 |$25,371 |$6,285 |
2004 |20th Century Fox |12,204 |$212,730 |$53,225 |
A |& |E Entertainment |4,628 |$120,891 |$31,856 |
Columbia Pictures |14,111 |$171,889 |$41,901 |
Disney Studios |8,727 |$147,355 |$37,042 |
Lyons Group |5,846 |$72,062 |$17,095 |
MGM Studios |7,989 |$106,029 |$25,096 |
Paramount Pictures |7,817 |$115,450 |$28,129 |
Sport Vision |917 |$17,654 |$4,515 |
TriStar Pictures |12,981 |$189,755 |$46,436 |
Universal Studios |10,804 |$160,960 |$40,143 |
Ventura Distribution |1,062 |$24,234 |$6,030 |
Music |2003 |BMG |23,954 |$331,871 |$29,541 |
Columbia House |6,788 |$91,776 |$9,148 |
Polygram |16,608 |$254,615 |$31,212 |
Sony Music |22,574 |$323,576 |$33,592 |
Virgin Records |18,716 |$284,133 |$33,905 |
WEA |11,284 |$165,230 |$22,617 |
2004 |BMG |22,257 |$307,634 |$27,357 |
Columbia House |6,350 |$86,428 |$8,725 |
Polygram |15,447 |$238,817 |$29,461 |
Sony Music |20,696 |$297,124 |$30,800 |
Virgin Records |17,381 |$263,071 |$30,997 |
WEA |10,071 |$148,059 |$20,441 |

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
anshumaEngineeringAuthor Commented:
The code works like a charm , thank you so much. There's only one issue

If one of the data values contains "&" then it's broken up too like this

Simon  |& | Schuster

But the Expected Output is

Simon & Schuster |

how can this be handled
0
 
anshumaEngineeringAuthor Commented:
Also it's missing the attributes and metrics in the top row

example 1
Call Center|Timeliness|

example 2
Category| Year| Supplier|Units Sold| Revenue| Profit|

So the code needs to handle attributes and metrics too. Can you please modify the code for that as well
0
 
for_yanCommented:
I don't see where I can get the titles

This ampersand is a big problem
there is something written about it in here in ahdnling special charcaters section,

http://docs.oracle.com/javase/tutorial/jaxp/sax/parsing.html

but seems too coplicated.
I'd rather re-write your fikle upfromt chaning "&amp; to "and" beforehand
0
 
anshumaEngineeringAuthor Commented:
the titles are here

if you open the file1.xml we are looking for "attribute" and "metric"

__________________________________________________________________________________
<attribute rfd="0" id="8D679D3511D3E4981000E787EC6DE8A4" name="Call Center" type="12">


<metric rfd="2" id="4C051EAF11D3E877C000B3B2D86C964F" name="Timeliness" type="4"><format cat="4" cp="0" ts="-1" nn="1" dp="2" cs="$">

__________________________________________________________________________________

<taskResponse statusCode="200">
<report_data_list scp="1" crt="1331655743000"><report_data locc="US" id="D1AE56B011D5C4D04C200E8820504F4F" locid="1033" name="Call Center Timeliness" locl="en" istab="1"><prs><pr v="0" n="LongNames" psn="Template Formatting"></pr><pr v="" n="VisProps" psn="Visualization"></pr></prs><styles><style id="c17"><prs n="FormattingNumber"><pr v="4" n="Category"></pr><pr v="2" n="DecimalPlaces"></pr><pr v="-1" n="ThousandSeparator"></pr><pr v="$" n="CurrencySymbol"></pr><pr v="0" n="CurrencyPosition"></pr><pr v="0.00%" n="Format"></pr><pr v="1" n="NegativeNumbers"></pr></prs></style><style id="c16"><prs n="FormattingNumber"><pr v="4" n="Category"></pr><pr v="2" n="DecimalPlaces"></pr><pr v="-1" n="ThousandSeparator"></pr><pr v="$" n="CurrencySymbol"></pr><pr v="0" n="CurrencyPosition"></pr><pr v="0.00%" n="Format"></pr><pr v="1" n="NegativeNumbers"></pr></prs></style></styles><objects><attribute rfd="0" id="8D679D3511D3E4981000E787EC6DE8A4" name="Call Center" type="12"><form rfd="1" id="CCFBE2A5EADB4F50941FB879CCF1721C" base_form_type="3" name="DESC" type="21"></form></attribute><metric rfd="2" id="4C051EAF11D3E877C000B3B2D86C964F" name="Timeliness" type="4"><format cat="4" cp="0" ts="-1" nn="1" dp="2" cs="$">0.00%</format></metric></objects><template><axis id="1"><unit rfd="0" unit_type="1"></unit></axis><axis id="2"><unit unit_type="3"><oi rfd="2"></oi></unit></axis><axis id="3"></axis></template><raw_data><headers><oi rfd="0"></oi><oi rfd="2"></oi></headers><rows cn="15">
0
 
for_yanCommented:
This will take care of Simon & Shuster:

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

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

/*
<product>
 <name>laptop</name>
 <price>120</price>
 <maker>Dell</maker>
</product>

*/
import java.util.ArrayList;
import java.util.HashMap;


public class ParseingXMLWithSAX {


   HashMap m = new HashMap();
    ArrayList<String> ar = null;
    String s;
    String key;

  

    String filename =  "xml2.txt";

    public  void getElementFromXML() {
        try {

               ar = new ArrayList<String>();
              s= "";
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            DefaultHandler handler = new DefaultHandler() {

                boolean length1 = false;

                


                public void startElement(String uri, String localName,
                        String qName, Attributes attributes)
                        throws SAXException {

                    if (qName.toUpperCase().equalsIgnoreCase("r")) {

                    }

                    if (qName.toUpperCase().equalsIgnoreCase("v")) {
                        length1 = true;
                    }




                    }
                public void endElement(String uri, String localName,
                        String qName) throws SAXException {

                    if (qName.toUpperCase().equalsIgnoreCase("v")) {
                        length1 = false;


                    }

                      if (qName.toUpperCase().equalsIgnoreCase("r")) {
                           //  System.out.println("");
                          ar.add(s);
                          s="";

                      }



                }
                public void characters(char ch[], int start, int length)
                        throws SAXException {



                   if (length1) {
                       
                     
                        //   System.out.print( new String(ch, start, length) + " |");
                       s+= new String(ch, start, length) + " |";
                    }


                    }


            };
            saxParser.parse(filename, handler);
            for(String s: ar){
                s = s.replace("|& |"," & ");
                System.out.println(s);

            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }





    }

    public static void main(String args[])
    {
        new ParseingXMLWithSAX().getElementFromXML();

    }


}

Open in new window



Ouytput
Books |2003 |Bantam Books |309 |$4,742 |$1,298 |
John Wiley   &  Sons |271 |$4,726 |$1,320 |
McGraw Hill |114 |$4,382 |$1,182 |
Perigee |334 |$3,015 |$779 |
Prentice Hall |186 |$2,370 |$637 |
Scribner |325 |$3,265 |$780 |
Simon   &  Schuster |214 |$3,218 |$870 |
Vintage Books |287 |$3,321 |$860 |
Warner Books |317 |$4,190 |$1,103 |
2004 |Bantam Books |275 |$4,327 |$1,176 |
John Wiley   &  Sons |206 |$3,468 |$968 |
McGraw Hill |92 |$3,843 |$1,035 |
Perigee |315 |$2,839 |$728 |
Prentice Hall |181 |$2,425 |$650 |
Scribner |311 |$3,139 |$751 |
Simon   &  Schuster |246 |$3,765 |$1,014 |
Vintage Books |278 |$3,234 |$827 |
Warner Books |288 |$3,952 |$1,069 |
Electronics |2003 |ACS Innovations |4,679 |$2,237,510 |$618,668 |
ATF Electronics |577 |$445,700 |$115,648 |
Audiotronics Inc. |2,027 |$407,290 |$109,823 |
Digital Equipment |756 |$64,960 |$17,436 |
Digital Office Inc. |789 |$269,030 |$71,177 |
DSS Appliance Co. |2,247 |$1,101,450 |$305,311 |
Entertaintron Inc. |2,180 |$597,860 |$162,497 |
Impact Components |1,029 |$188,253 |$51,574 |
MegaStore Corp. |819 |$423,213 |$116,456 |
Universal EL |478 |$217,718 |$60,335 |
2004 |ACS Innovations |4,042 |$1,858,920 |$509,858 |
ATF Electronics |598 |$462,500 |$119,981 |
Audiotronics Inc. |1,913 |$380,770 |$102,612 |
Digital Equipment |713 |$58,380 |$15,651 |
Digital Office Inc. |798 |$278,570 |$73,694 |
DSS Appliance Co. |2,086 |$1,036,450 |$287,678 |
Entertaintron Inc. |1,948 |$545,730 |$148,426 |
Impact Components |910 |$161,267 |$44,378 |
MegaStore Corp. |725 |$375,439 |$102,923 |
Universal EL |435 |$190,644 |$52,739 |
Movies |2003 |20th Century Fox |12,841 |$222,671 |$55,661 |
A  & E Entertainment |4,892 |$127,721 |$33,686 |
Columbia Pictures |15,315 |$185,855 |$45,249 |
Disney Studios |9,500 |$160,446 |$40,374 |
Lyons Group |6,414 |$78,758 |$18,691 |
MGM Studios |8,818 |$117,280 |$27,750 |
Paramount Pictures |8,533 |$126,180 |$30,731 |
Sport Vision |966 |$18,993 |$4,878 |
TriStar Pictures |13,716 |$200,425 |$48,996 |
Universal Studios |11,861 |$175,516 |$43,631 |
Ventura Distribution |1,163 |$25,371 |$6,285 |
2004 |20th Century Fox |12,204 |$212,730 |$53,225 |
A  & E Entertainment |4,628 |$120,891 |$31,856 |
Columbia Pictures |14,111 |$171,889 |$41,901 |
Disney Studios |8,727 |$147,355 |$37,042 |
Lyons Group |5,846 |$72,062 |$17,095 |
MGM Studios |7,989 |$106,029 |$25,096 |
Paramount Pictures |7,817 |$115,450 |$28,129 |
Sport Vision |917 |$17,654 |$4,515 |
TriStar Pictures |12,981 |$189,755 |$46,436 |
Universal Studios |10,804 |$160,960 |$40,143 |
Ventura Distribution |1,062 |$24,234 |$6,030 |
Music |2003 |BMG |23,954 |$331,871 |$29,541 |
Columbia House |6,788 |$91,776 |$9,148 |
Polygram |16,608 |$254,615 |$31,212 |
Sony Music |22,574 |$323,576 |$33,592 |
Virgin Records |18,716 |$284,133 |$33,905 |
WEA |11,284 |$165,230 |$22,617 |
2004 |BMG |22,257 |$307,634 |$27,357 |
Columbia House |6,350 |$86,428 |$8,725 |
Polygram |15,447 |$238,817 |$29,461 |
Sony Music |20,696 |$297,124 |$30,800 |
Virgin Records |17,381 |$263,071 |$30,997 |
WEA |10,071 |$148,059 |$20,441 |

Open in new window

0
 
anshumaEngineeringAuthor Commented:
I am getting a type mismatch error here for ar1 and ar

"Type mismatch: cannot convert from element type Object to String"

 for(String s: ar1){
               System.out.print(s + " |");

           }
           System.out.println("");
           for(String s: ar){
               s = s.replace("|& |"," & ");
               System.out.println(s);

           }
0
 
for_yanCommented:
Use exactly my code as I posted it in ID: 37735469
I compiled it with Java 6

If I didn't have type mismatch - you will not have it either.
Replace the whole code as a whole - that;s why I post it every time again.
0
 
for_yanCommented:
type mismatch can be here
   for(String s: ar){

 uif your arraylist is not decalred this way
ArrayList<String> ar

But no sense to deal withn this details - I compiled the code above - it should compile for you either
0
 
anshumaEngineeringAuthor Commented:
Cool !!! Works great. You rock

thanks
-anshu
0
 
anshumaEngineeringAuthor Commented:
Thanks a ton
0
 
for_yanCommented:
You are always welcome.
0
 
anshumaEngineeringAuthor Commented:
One last question, how can I avoid the last pipe "|" as it's redundant

Sony Music |20,696 |$297,124 |$30,800 |
Virgin Records |17,381 |$263,071 |$30,997 |
WEA |10,071 |$148,059 |$20,441 |


thanks
-anshu
0
 
for_yanCommented:
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

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


import java.util.ArrayList;
import java.util.HashMap;


public class ParseingXMLWithSAX {


   HashMap m = new HashMap();
    ArrayList<String> ar = null;
       ArrayList<String> ar1 = null;
    String s;
    String key;

  

    String filename =  "xml2.txt";

    public  void getElementFromXML() {
        try {

               ar = new ArrayList<String>();
                 ar1 = new ArrayList<String>();
              s= "";
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            DefaultHandler handler = new DefaultHandler() {

                boolean length1 = false;

                


                public void startElement(String uri, String localName,
                        String qName, Attributes attributes)
                        throws SAXException {

                    if (qName.toUpperCase().equalsIgnoreCase("r")) {

                    }

                    if (qName.toUpperCase().equalsIgnoreCase("v")) {
                        length1 = true;
                    }

                     if (qName.toUpperCase().equalsIgnoreCase("attribute")) {
                         ar1.add(attributes.getValue("name"));

                    }

                               if (qName.toUpperCase().equalsIgnoreCase("metric")) {
                         ar1.add(attributes.getValue("name"));

                    }




                    }
                public void endElement(String uri, String localName,
                        String qName) throws SAXException {

                    if (qName.toUpperCase().equalsIgnoreCase("v")) {
                        length1 = false;


                    }

                      if (qName.toUpperCase().equalsIgnoreCase("r")) {
                           //  System.out.println("");
                          ar.add(s);
                          s="";

                      }



                }
                public void characters(char ch[], int start, int length)
                        throws SAXException {



                   if (length1) {
                       
                     
                        //   System.out.print( new String(ch, start, length) + " |");
                       s+= new String(ch, start, length) + " |";
                    }


                    }


            };
            saxParser.parse(filename, handler);

            String s0="";
            for(String s: ar1){
                s0 += s + " |";


            }
           System.out.println(s0.substring(0,s0.length()-1));

          //  System.out.println("");
            for(String s: ar){
                s = s.replace("|& |"," & ");
                System.out.println(s.substring(0,s.length()-1));

            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }





    }

    public static void main(String args[])
    {
        new ParseingXMLWithSAX().getElementFromXML();

    }


}

Open in new window


Output:
Category |Year |Supplier |Units Sold |Revenue |Profit 
Books |2003 |Bantam Books |309 |$4,742 |$1,298 
John Wiley   &  Sons |271 |$4,726 |$1,320 
McGraw Hill |114 |$4,382 |$1,182 
Perigee |334 |$3,015 |$779 
Prentice Hall |186 |$2,370 |$637 
Scribner |325 |$3,265 |$780 
Simon   &  Schuster |214 |$3,218 |$870 
Vintage Books |287 |$3,321 |$860 
Warner Books |317 |$4,190 |$1,103 
2004 |Bantam Books |275 |$4,327 |$1,176 
John Wiley   &  Sons |206 |$3,468 |$968 
McGraw Hill |92 |$3,843 |$1,035 
Perigee |315 |$2,839 |$728 
Prentice Hall |181 |$2,425 |$650 
Scribner |311 |$3,139 |$751 
Simon   &  Schuster |246 |$3,765 |$1,014 
Vintage Books |278 |$3,234 |$827 
Warner Books |288 |$3,952 |$1,069 
Electronics |2003 |ACS Innovations |4,679 |$2,237,510 |$618,668 
ATF Electronics |577 |$445,700 |$115,648 
Audiotronics Inc. |2,027 |$407,290 |$109,823 
Digital Equipment |756 |$64,960 |$17,436 
Digital Office Inc. |789 |$269,030 |$71,177 
DSS Appliance Co. |2,247 |$1,101,450 |$305,311 
Entertaintron Inc. |2,180 |$597,860 |$162,497 
Impact Components |1,029 |$188,253 |$51,574 
MegaStore Corp. |819 |$423,213 |$116,456 
Universal EL |478 |$217,718 |$60,335 
2004 |ACS Innovations |4,042 |$1,858,920 |$509,858 
ATF Electronics |598 |$462,500 |$119,981 
Audiotronics Inc. |1,913 |$380,770 |$102,612 
Digital Equipment |713 |$58,380 |$15,651 
Digital Office Inc. |798 |$278,570 |$73,694 
DSS Appliance Co. |2,086 |$1,036,450 |$287,678 
Entertaintron Inc. |1,948 |$545,730 |$148,426 
Impact Components |910 |$161,267 |$44,378 
MegaStore Corp. |725 |$375,439 |$102,923 
Universal EL |435 |$190,644 |$52,739 
Movies |2003 |20th Century Fox |12,841 |$222,671 |$55,661 
A  & E Entertainment |4,892 |$127,721 |$33,686 
Columbia Pictures |15,315 |$185,855 |$45,249 
Disney Studios |9,500 |$160,446 |$40,374 
Lyons Group |6,414 |$78,758 |$18,691 
MGM Studios |8,818 |$117,280 |$27,750 
Paramount Pictures |8,533 |$126,180 |$30,731 
Sport Vision |966 |$18,993 |$4,878 
TriStar Pictures |13,716 |$200,425 |$48,996 
Universal Studios |11,861 |$175,516 |$43,631 
Ventura Distribution |1,163 |$25,371 |$6,285 
2004 |20th Century Fox |12,204 |$212,730 |$53,225 
A  & E Entertainment |4,628 |$120,891 |$31,856 
Columbia Pictures |14,111 |$171,889 |$41,901 
Disney Studios |8,727 |$147,355 |$37,042 
Lyons Group |5,846 |$72,062 |$17,095 
MGM Studios |7,989 |$106,029 |$25,096 
Paramount Pictures |7,817 |$115,450 |$28,129 
Sport Vision |917 |$17,654 |$4,515 
TriStar Pictures |12,981 |$189,755 |$46,436 
Universal Studios |10,804 |$160,960 |$40,143 
Ventura Distribution |1,062 |$24,234 |$6,030 
Music |2003 |BMG |23,954 |$331,871 |$29,541 
Columbia House |6,788 |$91,776 |$9,148 
Polygram |16,608 |$254,615 |$31,212 
Sony Music |22,574 |$323,576 |$33,592 
Virgin Records |18,716 |$284,133 |$33,905 
WEA |11,284 |$165,230 |$22,617 
2004 |BMG |22,257 |$307,634 |$27,357 
Columbia House |6,350 |$86,428 |$8,725 
Polygram |15,447 |$238,817 |$29,461 
Sony Music |20,696 |$297,124 |$30,800 
Virgin Records |17,381 |$263,071 |$30,997 
WEA |10,071 |$148,059 |$20,441 

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.