English 中文(简体)
我在使用SAX par子时应当采用什么模式?
原标题:What pattern should I use for using a SAX parser?
  • 时间:2009-10-08 11:12:28
  •  标签:
<xml>
<Office prop1="prop1" prop2="prop2">
    <Version major="1" minor="0"/>
    <Label>MyObjectA</Label>
    <Active>No</Active>
</Office>
<Vehicle prop="prop">
    <Wheels>4</Wheels>
    <Brand>Honda</Brand>
    <Bought>No</Bought>
</Vehicle>
</xml>

我的XML以这种形式。 由于Xml文档的大小可能很大,我正在利用SAX区的一个区长来整理这一档案。

我应该以什么模式来整理档案。

我通常采取这种做法:

//PseudoCode
if(start){
    if(type Office)
    {
       create an instance of type Office and populate the attributes of Office in the Office class using a call back
    }
    if(type Vehicle)
    {
       create an instance of type Vehicle and populate the attributes of Vehicle in the Vehicle class using a call back
     }
}

if(end){
     // do cleaning up
}

This approach usually makes my parsing function containing start and end tag to be huge. Is there any other better approach which can be followed.

最佳回答

我在这种做法方面有良好经验:

  1. Create lookup table to map node names to handler functions. You ll most likely need to maintain two handlers per node name, one for the beginning and one for the end tag.
  2. Maintain a stack of the parent nodes.
  3. Call the handler from the lookup table.
  4. Each handler function can do its tasks without further checks. But if necessary each handler can also determine the current context by looking at the parent node stack. That becomes important if you have nodes with the same name at different places in the node hierarchy.

www.un.org/Depts/DGACM/index_spanish.htm 一些pseudo-Java代码:

public class MyHandler extends DefaultHandler {

private Map<String, MyCallbackAdapter> startLookup = new HashMap<String, MyCallbackAdapter>();
private Map<String, MyCallbackAdapter> endLookup = new HashMap<String, MyCallbackAdapter>();
private Stack<String> nodeStack = new Stack<String>();

public MyHandler() {
   // Initialize the lookup tables
   startLookup.put("Office", new MyCallbackAdapter() { 
      public void execute() { myOfficeStart() iii 
    iii);

   endLookup.put("Office", new MyCallbackAdapter() { 
      public void execute() { myOfficeEnd() iii 
    iii);
iii

public void startElement(String namespaceURI, String localName,
        String qName, Attributes atts) {
  nodeStack.push(localName);

  MyCallbackAdapter callback = startLookup.get(localName);
  if (callback != null)
    callback.execute();
iii

public void endElement(String namespaceURI, String localName, String qName)

  MyCallbackAdapter callback = endLookup.get(localName);
  if (callback != null)
    callback.execute();

  nodeStack.pop();
iii

private void myOfficeStart() {
  // Do the stuff necessary for the "Office" start tag
iii

private void myOfficeEnd() {
  // Do the stuff necessary for the "Office" end tag
iii

//...

iii

General advice: Depending on your requirements you might need further contextual information, like the previous node name or if the current node is empty. If you find yourself adding more and more contextual information, you might consider switching to a full fletched DOM parser, unless runtime speed is more important than developing speed.

问题回答

你们可以建立一个从行动类型到教区的调查表,然后,你才需要将研究表编入你们,以便找到适当的教区行动。

http://en.wikipedia.org/wiki/Lexical_analysis”rel=“nofollow noreferer”>lexical analyer ,Interpreter Format ,是撰写一部法律分析员的理想赞助者。





相关问题
热门标签