简体   繁体   中英

Parsing nested elements in an XML file using Java

Currently, this is what my XML file looks like. There's 10 more students, but I figured you didn't need to see that.

<Student id="11111">
    <FirstName>Joe</FirstName>
    <LastName>Edwards</LastName>
    <Address>
        <Street>345 North</Street>
        <City>Brea</City>
        <State>CA</State>
        <Zip>99999</Zip>
        <Phone>(714) 444-4444</Phone>
    </Address>
    <Classes>
        <Class crn="5555">
            <Name>CIS 100</Name>
            <Units>3</Units>
            <Grade>A</Grade>
        </Class>
        <Class crn="7777">
            <Name>CIS 111</Name>
            <Units>4</Units>
            <Grade>B</Grade>        
        </Class>
    </Classes>
</Student>

And this is my code to 'extract' the first and last names, respectively.

NodeList studentlist = doc.getElementsByTagName("Student");

        for(int i = 0; i < studentlist.getLength(); i++)
        {
            Node n = studentlist.item(i); 
            Element element = (Element)n; 
            String id = element.getAttribute("id");
            Student obj = new Student(id);
            NodeList slist = n.getChildNodes();

            for (int j = 0; j < slist.getLength(); j++)
            {
                Node selement = slist.item(j);

                if (selement.getNodeType() == Node.ELEMENT_NODE)
                {
                    String textval = selement.getTextContent();

                    if (selement.getNodeName().equals("FirstName"))
                    {
                        obj.setFirstname(textval);
                    }
                    if (selement.getNodeName().equals("LastName"))
                    {
                        obj.setLastname(textval);
                    }
                }
            }

            students.add(obj);
        }

And my Student class:

public class Student 
{
    private String id;
    private String firstname;
    private String lastname;
    private Address adr;
    private ArrayList<Course> courses;

My question is this: how do I add the 'address' and 'classes' nodes for this same object?

So, one way to approach this is to create Data structures for both Address and Class, like so:

public class Address
{
    private String street;
    private String city;
    private String state;
    private String zip;
    private String phone;
}

and (note: calling a Java Class "Class" may prove to be very confusing, so use something else I'd say)

public class Course
{
    private int crn;
    private String name;
    private String units;
    private String grade;
}

(and you'll have to create constructors for both of those classes too)

Then, in your for-loop, in the outer "if" block, after the "if" statements that check the node names for "FirstName" and "LastName", you could do something like:

if (selement.getNodeName().equals("Address"))
{
    String street = null;
    String city = null;
    String state = null;
    String zip = null;
    String phone = null;
    NodeList addressChildren = selement.getChildNodes();
    for(int k=0; k < addressChildren.getLength(); k++) {
        Node addrNode = addressChildren.item(k);
        if(addrNode.getNodeName().equals("Street")
        {
           street = addrNode.getTextContent();
        }
        ...//etc for City, State, Zip, and Phone
        Address a = new Address(street, city, state, zip, phone)
        obj.setAddress(a);
    }
}
if (selement.getNodeName().equals("Classes"))
{
    NodeList classNodes = selement.getChildNodes();
    Course[] courses = new Course[classNodes.getLength());
    for(int l=0; l < classNodes.getLength(); l++) {
        Node classNode = classNodes.item(l);
        NodeList classChildren = classNode.getChildNodes();
        for(int m=0; m < classChildren.getLenght(); m++)
        {
            Node classChild = classChildren.item(i);
            int crn = Integer.parseInt(classChild.getAttributes().getNamedItem("crn").getTextContent());
            String name = null;
            String units = null;
            if(classChild.getNodeName().equals("Name")
            {
               name = classChild.getTextContent();
            }
            if(classChild.getNodeName().equals("Units")
            {
               units = classChild.getTextContent();
            }
            courses[m] = new Course(crn, name, units);
        }
        obj.setCourses(Arrays.asList(courses));
    }
}

Ok, so the code above isn't perfect (I wrote it in the browser), but I think, based on what you've already figured out about XML parsing, that this is a good start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM