0

I am trying replace the arrays I have with arraylists so I don't have to worry about managing the arrays and can clean up my code. I have only just taught myself arraylists so I am having some issues. Mainly in these classes where all my "sets" and "gets" are. I don't think I have the right syntax because I am getting an ArrayList/String conflict error. Basically trying to get:

public static ArrayList<Resource> importResourcesFromXML(String documentLocation)

to import an arraylist of resources from an XML document. I also wanted to convert my arrays in Resources and my T_Resources into arraylists as well. This is the code I have so far, I believe I have Resources implemented correctly but could use help with the other two getting the XML resources to display properly.

EDIT: I want to if at all possible eliminate the array entirely and replace it with an arraylist. I want to try and avoid converting an array to an arraylist.

import java.util.ArrayList;


public class Resources {

//private static final int  MAX_SUBJECTS = 20;
private String              title;  
private String              description;
private Identifier          identifier;
ArrayList<Subject> subject = new ArrayList<Subject>();
//private int               subjectCount;


public Resources() {

    title           = "unknown title";
    description     = "unknown description";
    identifier      = null;

    //subjects      = new Subject[MAX_SUBJECTS];
    //subjectCount  = 0;

}

public void setTitle(String newTitle) {

    title = newTitle;

}

public String getTitle() {

    return title;

}

public void setDescription(String newDescription) {

    description = newDescription;

}

public String getDescription() {

    return description;

}

public void setIdentifier(Identifier newIdentifier) {

    identifier = newIdentifier;

}

public Identifier getIdentifier() {

    return identifier;

}

public void addSubject(Subject newSubject) {

    subject.add(newSubject);

}

public ArrayList<Subject> getSubjects() {

    //Subject[] result = new Subject[subjectCount];


    //System.arraycopy(subjects, 0, result, 0, subjectCount);

    return subject;
}

}

public class ResourceImporter {

// This operation loads the XML document specified by the document location, which can a file or a URL,
// and returns a reference to the document. If the operation cannot successfully load the document
// the operation returns the null reference.
//
private static Document loadXMLDocument(String documentLocation) {

    // The XML document.
    //
    Document documentIn     = null;

    // The parser that reads in an XML files.
    //
    DocumentBuilder parser  = null;

    // Pull the document
    //
    try {

        // Obtain a document parser.
        //
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        builderFactory.setNamespaceAware(true);

        parser = builderFactory.newDocumentBuilder();

        documentIn = parser.parse(documentLocation);

    } catch (ParserConfigurationException p) {

        System.out.println("Error creating parser.");
        System.out.println("   " + p.getMessage());

    } catch (SAXException s) {

        System.out.println("Document is not well formed.");
        System.out.println("   " + s.getMessage());

    } catch (IOException i) {

        System.out.println("Error accessing the file.");
        System.out.println("   " + i.getMessage());

    } catch (Exception e) {

        System.out.println("Unknown error occurred.");
        System.out.println("   " + e.getMessage());

    }


    return documentIn;

}

public static ArrayList<Resource> importResourcesFromXML(String documentLocation) { 

    ArrayList<Resource> resource = new ArrayList<Resource>();

    Document doc;

    Element resourceElement;

    Element titleElement;
    String title;

    Element descriptionElement;
    String description;

    Element identifierElement;
    String identifiers;

    Element urlElement;
    String url;

    NodeList subjectList;

    Element subjectElement;
    String subjects;

    Element categoryElement;
    String category;

    Element subcategoryElement;
    String subcategory;


    doc = loadXMLDocument(documentLocation);

    resourceElement = (Element)doc.getElementsByTagName("resource").item(0);


    if (resourceElement != null) {

        titleElement = (Element)resourceElement.getElementsByTagName("title").item(0);  

        resource.setTitle( titleElement == null ? "unknown" : titleElement.getTextContent() );

        descriptionElement = (Element)resourceElement.getElementsByTagName("description").item(0);

        resource.setDescription( descriptionElement == null ? "unknown" : descriptionElement.getTextContent() );

        identifierElement = (Element)resourceElement.getElementsByTagName("identifier").item(0);


        if (identifierElement != null) {

            Identifier identifier = new Identifier();

            urlElement = (Element)identifierElement.getElementsByTagName("url").item(0);

            identifier.setURL( urlElement == null ? "unknown" : urlElement.getTextContent() );

            resource.setIdentifier(identifier);

            subjectElement = (Element)resourceElement.getElementsByTagName("subjects").item(0);

            if (subjectElement != null) {

                subjectList = subjectElement.getElementsByTagName("subject");

                for (int i=0; i < subjectList.getLength(); ++i) {

                    Subject subject = new Subject();

                    subjectElement = (Element)subjectList.item(i);

                    categoryElement = (Element)subjectElement.getElementsByTagName("category").item(0);

                    subject.setCategory( categoryElement == null ? "unknown"  : categoryElement.getTextContent() );

                    subcategoryElement = (Element)subjectElement.getElementsByTagName("subcategory").item(0);

                    subject.setSubcategory( subcategoryElement == null ? "unknown" :subcategoryElement.getTextContent() );

                    resource.addSubject(subject);

                }
            }

        }
    }

    return resource;

}

}

import java.util.ArrayList;


public class T_Resources {

public static void main(String[] args) {

    ArrayList<Resource> resource = ResourceImporter.importResourcesFromXML("http://free1.ed.gov/xml/gemexport.xml");

    displayResources(resource);

}

private static void displayResources(ArrayList<Resource> resource) {

    ArrayList<Subject> subjects;

    System.out.println(resource.getTitle());

    System.out.println(resource.getDescription());

    System.out.println(resource.getIdentifier().getURL());

    subjects = resource.getSubjects();

    for (int i=0; i < subjects.size(); ++i) {

        System.out.println(subjects.getCategory() + " :: " + subjects.getSubcategory());

    }

    System.out.println();
}

}

public class Subject {

private String category;
private String subcategory;

public Subject() {

    String category         = "unknown";
    String subcategory      = "unknown";

}

public Subject(Subject subject) {

    category        = subject.category;
    subcategory     = subject.subcategory;

}

public void setCategory(String newCategory) {

    category = (newCategory == null) ? "unknown" : newCategory;

}

public String getCategory() {

    return category;

}

public void setSubcategory(String newSubcategory) {

    subcategory = newSubcategory;

}

public String getSubcategory() {

    return subcategory;

}
}

public class Identifier {

private String url;

public Identifier() {

    url = "unknown";

}

public void setURL(String newURL) {

    url = newURL;

}

public String getURL() {

    return url;

}

}
4

1 に答える 1