代码之家  ›  专栏  ›  技术社区  ›  akappa

无法使用SAX读取某些属性

  •  4
  • akappa  · 技术社区  · 15 年前

    我试图用SAX解析该文档:

    <scxml version="1.0" initialstate="start" name="calc"> 
      <datamodel> 
          <data id="expr" expr="0" /> 
          <data id="res" expr="0" /> 
      </datamodel> 
      <state id="start"> 
          <transition event="OPER" target="opEntered" /> 
          <transition event="DIGIT" target="operand" /> 
      </state> 
      <state id="operand"> 
          <transition event="OPER" target="opEntered" /> 
          <transition event="DIGIT" /> 
      </state> 
    </scxml>
    

    我很好地阅读了所有的属性,除了“缩写”和“名称”… 我使用startelement处理程序获取属性,但scxml的属性列表的大小为零。为什么?我怎样才能克服这个问题?

    编辑 :

    public void startElement(String uri, String localName, String qName, Attributes attributes){
      System.out.println(attributes.getValue("initialstate"));
      System.out.println(attributes.getValue("name")); 
    }
    

    当解析第一个标签时,这不起作用(两次打印“空”)。事实上,

    attributes.getLength();
    

    计算结果为零。

    谢谢

    2 回复  |  直到 14 年前
        1
  •  3
  •   rochb    15 年前

    我有一个完整的例子 there 并将其改编为您的文件:

    public class SaxParserMain {
    
        /**
         * @param args
         * @throws SAXException
         * @throws ParserConfigurationException
         * @throws IOException
         */
        public static void main(String[] args) throws ParserConfigurationException, SAXException,
                IOException {
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            CustomHandler handler = new CustomHandler();
            parser.parse(new File("file/scxml.xml"), handler);
        }
    }
    

    public class CustomHandler extends DefaultHandler {
    
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {
            System.out.println();
            System.out.print("<" + qName + "");
            if (attributes.getLength() == 0) {
                System.out.print(">");
            } else {
                System.out.print(" ");
                for (int index = 0; index < attributes.getLength(); index++) {
                    System.out.print(attributes.getLocalName(index) + " => "
                            + attributes.getValue(index));
                }
                System.out.print(">");
            }
        }
    
        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            System.out.print("\n</" + qName + ">");
        }
    }
    

    输出为:

    <scxml version => 1.0initialstate => startname => calc>
    <datamodel>
    <data id => exprexpr => 0>
    </data>
    <data id => resexpr => 0>
    </data>
    </datamodel>
    <state id => start>
    <transition event => OPERtarget => opEntered>
    </transition>
    <transition event => DIGITtarget => operand>
    </transition>
    </state>
    <state id => operand>
    <transition event => OPERtarget => opEntered>
    </transition>
    <transition event => DIGIT>
    </transition>
    </state>
    </scxml>
    
        2
  •  1
  •   skaffman    15 年前

    Attributes.getValue() 不像看上去那么简单。这个 javadoc 说:

    按XML查找属性的值 限定(前缀)名称。

    因此,如果存在任何名称空间复杂的情况,只传入“initialState”可能不起作用,因为“initialState”在技术上不是限定名。

    我建议用其他方法 Attributes 类,如 getValue(int) 你可能会在这些方面取得更大的成功。


    编辑 :另一种可能是调用 startElement 不是指你认为的元素。你确认了吗 localName 争论是真的 scxml 不是别的吗?