如何在Java SAX中解析此XML?

时间:2011-11-18 01:08:36

标签: java xml

我在java中使用SAX解析器。我不确定:

1)这种情况需要哪些课程?我猜我想拥有 课程(如果我的想法完全错误,请告诉我): -FosterHome(包含家庭和儿童的Arraylist) -Family(包含Child的ArrayList和父级的String) -Child(包含ChildID的ArrayList)

2)如何在startElement和endElement方法中处理这种情况

由于ChildID出现在ChildList和RemainingChildList中,导致复杂化。感谢任何可以帮助我的人。

<FosterHome>
<Orphanage>Happy Days Daycare</Orphanage>
<Location>Apple Street</Location>
<Families>
    <Family>
        <Parent>Adams</ParentID>
        <ChildList>
            <ChildID>Child1</ChildID>
            <ChildID>Child2</ChildID>
        </ChildList>
    </Family>
    <Family>
        <Parent>Adams</ParentID>
        <ChildList>
            <ChildID>Child3</ChildID>
            <ChildID>Child4</ChildID>
        </ChildList>
    </Family>
</Families>
<RemainingChildList>
<ChildID>Child5</ChildID>
<ChildID>Child6</ChildID>
</RemainingChildList>
</FosterHome>

2 个答案:

答案 0 :(得分:2)

如果您的文档很小,我首先要说的是:不要使用SAX解析器,而是使用DOM解析器。

如果您的文档很大,或者“即时​​”阅读,请使用SAX解析器。 SAX解析器的缺点是你负责提供与XML相当的代码,所以如果你想要自己的对象中的每个元素,你将不得不自己定义所有这些类。

class FosterHome {
  String orphanage;
  ArrayList<Family> families = new ArrayList<Family>();
  ArrayList<Child> remainingChildren = new ArrayList<Child>();
}

class Family {
  String parent;
  ArrayList<Child> children = new ArrayList<Child>();
  public Family(String _parent) { parent = _parent; }
}

class Child {
  String id;
  public Child(String _id) { id = _id;}
}

etc.

然后在看到开始和结束标记时为SAX事件编写事件处理程序,它们跟踪当前打开的元素是什么,在找到它们时向它们添加对象,并在找到结束标记时关闭它们。

这是一项苦差事,但在各个地方都有详细记录,例如http://totheriver.com/learn/xml/xmltutorial.html

答案 1 :(得分:1)

要处理来自不同父元素的 ChildID 元素,您可以在为输入 startElement() 处理程序方法时保存当前状态ChildList RemainingChildList 。然后,当您为 ChildID 输入 endElement() 处理程序方法时,您将处理当前的 ChildID ,具体取决于保存状态(例如填充FosterHome类中的相应字段)。

例如:

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

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

import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.IOException;

public class XmlParser {

static void parse(String xml, Handler handler) throws SAXException, ParserConfigurationException, IOException
{
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    XMLReader reader = saxParserFactory.newSAXParser().getXMLReader();
    reader.setContentHandler(handler);
    reader.parse(new InputSource(new CharArrayReader(xml.toCharArray())));
}

static class Handler extends DefaultHandler {

    CharArrayWriter contents = new CharArrayWriter();

    static enum STATE { Family, Remaining }
    STATE state;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
    {
        if (qName.equals("ChildList"))
            state = STATE.Family;
        else if (qName.equals("RemainingChildList"))
            state = STATE.Remaining;
        else if (qName.equals("ChildID"))
            contents.reset();
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException
    {
        if (qName.equals("ChildID"))
            System.out.println(contents.toString() + " [" + state + "]");
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException
    {
        contents.write(ch, start, length);
    }
}

public static void main(String[] args) throws Exception
{
    String xml = "<FosterHome>\n" +
        "<Orphanage>Happy Days Daycare</Orphanage>\n" +
        "<Location>Apple Street</Location>\n" +
        "<Families>\n" +
        "    <Family>\n" +
        "        <ParentID>Adams</ParentID>\n" +
        "        <ChildList>\n" +
        "            <ChildID>Child1</ChildID>\n" +
        "            <ChildID>Child2</ChildID>\n" +
        "        </ChildList>\n" +
        "    </Family>\n" +
        "    <Family>\n" +
        "        <ParentID>Adams</ParentID>\n" +
        "        <ChildList>\n" +
        "            <ChildID>Child3</ChildID>\n" +
        "            <ChildID>Child4</ChildID>\n" +
        "        </ChildList>\n" +
        "    </Family>\n" +
        "</Families>\n" +
        "<RemainingChildList>\n" +
        "<ChildID>Child5</ChildID>\n" +
        "<ChildID>Child6</ChildID>\n" +
        "</RemainingChildList>\n" +
        "</FosterHome>";

    parse(xml, new Handler());
}
}