Benjamin Bolyard Ответов: 1

Как получить правильный результат. Ява


This Java file is suppose to run and pull the information from the legoSets.XML and show me the output. Right now the output I am getting is sum-num only. it does not display any information of the other 3 i need to see. (name, year, num-parts) I have comment out set-num to see if i can get name to put out results but it still only shows set-num.

    ---Desired Output---
        <set-num>00-1</set-num>
        <name>WEETABIX CASTLE</name>
        <year>1970</year>
        <num-parts>471</num-parts>>

But My output shows only the set-num

    ---real output---
        1: 00-1
        2: 0011-2
        3: 0011-3
        4: 0012-1
        5: 0013-1

    ---MAIN.Java---
    package assign6;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.XMLReaderFactory;
    
    /**
     *
     * @author assign
     */
    public class Assign6 {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            System.out.print("Welcome to Brad Pitt's Lego Sorter");
            try {
                XMLReader reader = XMLReaderFactory.createXMLReader();
                MyHandler handler = new MyHandler();
                reader.setContentHandler(handler);
                reader.setErrorHandler(handler);
                
                InputSource inputSource = new InputSource("legoSets.xml");
                reader.parse(inputSource);
                
                ArrayList<String> data = handler.getsetnums();
                
                System.out.println("set-num List");
                System.out.println("----------");
                for (int i = 0; i < data.size(); i++) {
                    String setnum = data.get(i);
                    setnum = setnum.toUpperCase();
                    System.out.println((i + 1) + ": " + setnum);
                }
                
                ArrayList<String> names = handler.getnames();
                
                System.out.println("name List");
                System.out.println("----------");
                for (int i = 0; i < data.size(); i++) {
                    String name = data.get(i);
                    name = name.toUpperCase();
                    System.out.println((i + 1) + ": " + name);
                }
                
                ArrayList<String> years = handler.getyears();
                
                System.out.println("year List");
                System.out.println("----------");
                for (int i = 0; i < data.size(); i++) {
                    String year = data.get(i);
                    year = year.toUpperCase();
                    System.out.println((i + 1) + ": " + year);
                }
                
                ArrayList<String> numparts = handler.getnumparts();
                
                System.out.println("num-part List");
                System.out.println("----------");
                for (int i = 0; i < data.size(); i++) {
                    String numpart = data.get(i);
                    numpart = numpart.toUpperCase();
                    System.out.println((i + 1) + ": " + numpart);
                }
                
            } catch (SAXException ex) {
                Logger.getLogger(Assign6.class.getName())
                        .log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Assign6.class.getName())
                        .log(Level.SEVERE, null, ex);
            }
        }
        
    }
    
    ---MyHandler.Java---
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package assign6;
    
    import java.util.ArrayList;
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import org.xml.sax.helpers.DefaultHandler;
    
    /**
     *
     * @author assign
     */
    public class MyHandler extends DefaultHandler {
    
        private boolean inset;
        private boolean insetnum;
        private boolean inname;
        private boolean inyear;
        private boolean innumpart;
        private String setnumBuffer;
        private String nameBuffer;
        private String yearBuffer;
        private String numpartBuffer;
        
         private ArrayList<String> setnums, names, years, numparts;
        
        public ArrayList<String> getsetnums() {
            return setnums;
        
        }
        
        public ArrayList<String> getnames() {
            return names;
        }
        
        public ArrayList<String> getyears() {
            return years;
        }
        public ArrayList<String> getnumparts() {
            return numparts;
        }
    
        @Override
        public void startDocument() throws SAXException {
            // System.out.println("Start Document")
            inset = false;
            insetnum = false;
            inname = false;
            inyear = false;
            innumpart = false;
            setnumBuffer = "";
            setnums = new ArrayList<>();
            nameBuffer = "";
            names = new ArrayList<>();
            yearBuffer = "";
            years = new ArrayList<>();
            numpartBuffer = "";
            numparts = new ArrayList<>();
        }
    
        @Override
        public void startElement(String uri, String localName,
                String qName, Attributes attributes) throws SAXException {
            if (localName.equals("lego-sets")) {
                inset = true;
            } else if (inset && localName.equals("set-num")) {
                // System.out.println("Start element : " + localName);
                insetnum = true;
                setnumBuffer = "";
            
            } else if (inset && localName.equals("name")) {
                // System.out.println("Start element : " + localName);
                inname = true;
                nameBuffer = "";
                
            } else if (inset && localName.equals("year")) {
                // System.out.println("Start element : " + localName);
                inyear = true;
                yearBuffer = "";
            } else if (inset && localName.equals("num-parts")) {
                // System.out.println("Start element : " + localName);
                innumpart = true;
                numpartBuffer = "";
            }
        }
    
        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            String data = new String(ch, start, length);
            if (inset && insetnum) {
                // System.out.println("Characters : " + data);
                setnumBuffer = setnumBuffer + data;
            } else if (inset && inname) {
                // System.out.println("Characters : " + data);
                nameBuffer = nameBuffer + data;
            } else if (inset && inyear) {
                // System.out.println("Characters : " + data);
                yearBuffer = yearBuffer + data;
            } else if (inset && innumpart) {
                // System.out.println("Characters : " + data);
                numpartBuffer = numpartBuffer + data;
        }
        }     
        @Override
        public void endElement(String uri, String localName,
                String qName) throws SAXException {
            if (localName.equals("inset")) {
                inset = false;
            } else if (inset && localName.equals("set-num")) {
                // System.out.println("Title: " + titleBuffer);
                // System.out.println("End element   : " + localName);
                // System.out.println();            
                setnums.add(setnumBuffer);
                insetnum = false;
            } else if (inset && localName.equals("name")) {
                // System.out.println("Title: " + titleBuffer);
                // System.out.println("End element   : " + localName);
                // System.out.println();            
                names.add(nameBuffer);
                inname = false;
            } else if (inset && localName.equals("year")) {
                // System.out.println("Title: " + titleBuffer);
                // System.out.println("End element   : " + localName);
                // System.out.println();            
                years.add(yearBuffer);
                inyear = false;
            } else if (inset && localName.equals("num-parts")) {
                // System.out.println("Title: " + titleBuffer);
                // System.out.println("End element   : " + localName);
                // System.out.println();            
                numparts.add(numpartBuffer);
                innumpart = false;
            }
        }
    
        @Override
        public void endDocument() throws SAXException {
            // System.out.println("End Document");
        }
    
        @Override
        public void error(SAXParseException e) throws SAXException {
            System.out.println("ERROR: Line number " + e.getLineNumber());
            System.out.println("ERROR:             " + e.getMessage());
        }
    
        @Override
        public void fatalError(SAXParseException e) throws SAXException {
            System.out.println("FATAL ERROR: Line number " + e.getLineNumber());
            System.out.println("FATAL ERROR:             " + e.getMessage());
        }
    
    }
    
    ---legoSets.xml---
    <lego-sets>
        <set>
            <set-num>00-1</set-num>
            <name>WEETABIX CASTLE</name>
            <year>1970</year>
            <num-parts>471</num-parts>
        </set>
        <set>
            <set-num>0011-2</set-num>
            <name>TOWN MINI-FIGURES</name>
            <year>1978</year>
            <num-parts>12</num-parts>
        </set>
        <set>
            <set-num>0011-3</set-num>
            <name>CASTLE 2 FOR 1 BONUS OFFER</name>
            <year>1987</year>
            <num-parts>2</num-parts>
        </set>
        <set>
            <set-num>0012-1</set-num>
            <name>SPACE MINI-FIGURES</name>
            <year>1979</year>
            <num-parts>12</num-parts>
        </set>
    </lego-sets>


Что я уже пробовал:

измените порядок что то.Java-файлы загружаются из XML. попробовал какое-то онлайн-решение.

1 Ответов

Рейтинг:
6

Benjamin Bolyard

The answer to the question is this. The reason name never pull up the needed information is due to it being inform wrong. data needs to be replace with names. Ones that has been done it can search and retrieve the correct information, the same goes for year and num-parts, the data just needs to be replace.

Incorrect

ArrayList<String> names = handler.getnames();

            System.out.println("name List");
            System.out.println("----------");
            for (int i = 0; i < **data**.size(); i++) {
                String name = **data**.get(i);
                name = name.toUpperCase();
                System.out.println((i + 1) + ": " + name);
Correctly Inform

ArrayList<String> names = handler.getnames();

            System.out.println("name List");
            System.out.println("----------");
            for (int i = 0; i < **names**.size(); i++) {
                String name = **names**.get(i);
                name = name.toUpperCase();
                System.out.println((i + 1) + ": " + name);

Credit goes to Kelvin @ tutor.com


Patrice T

Примите ваше решение, если оно будет принято.
от закроет этот вопрос.