我在Java中使用SAX解析器。我不确定:
1)在这种情况下我需要什么班级?我想我想拥有
的课程(请让我知道我的想法是否完全错误):
-FosterHome(包含家庭和儿童的数组列表)
-Family(包含ArrayList的Child和一个String的parent)
-Child(包含ArrayList作为ChildID)
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>
最佳答案
要处理来自不同父元素的ChildID元素,可以分别在输入ChildList和RemainingChildList的startElement()处理程序方法时保存当前状态。然后,当您输入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());
}
}
关于java - 如何在Java SAX中解析此XML?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8176590/