2010-01-13 92 views
3

我试图解析文档使用SAX:无法读取某些属性与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(); 

评估为零。

感谢

+0

我猜有些代码将受到欢迎。 – rochb 2010-01-13 11:35:20

+0

public void startElement(String uri,String localName,String qName,Attributes attributes){System.out.println(attributes.getValue(“initialstate”)); System.out.println(attributes.getValue(“name”)); } 解析第一个标签时不起作用。 – akappa 2010-01-13 11:37:53

+0

文档中是否有任何XML名称空间? – skaffman 2010-01-13 11:42:01

回答

2

我已经有了一个完整的例子来自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> 
+1

恩,谢谢,问题解决了。 问题是sax总是使用相同的属性实例,每当它引发一个事件时就会填充它。我认为它总是一个/不同/实例。叹! :) – akappa 2010-01-13 12:17:18

+0

是的,javadoc说:“atts - 元素附加的属性,如果没有属性,它应该是一个空的Attributes对象**在startElement返回后,这个对象的值是未定义的**” – Andreas 2015-08-20 16:00:49

1

Attributes.getValue()并不像它看起来那样简单。 javadoc说:

通过XML查找属性的值 合格的(前缀)名称。

因为“initialstate”在技术上不是合格的名称,所以只传递“initialstate”可能不起作用,如果有任何命名空间的复杂性。

我建议你在Attributes课上玩其他方法,比如getValue(int),你可能会有更多的成功。


编辑:另一种可能性是,这个调用startElement是不是指你认为它是元素。你是否证实localName的论点确实是scxml,而不是别的?

+0

问题是,第一个标记的属性列表的长度为零... – akappa 2010-01-13 11:45:53

+0

是的,localName是scxml。 – akappa 2010-01-13 11:54:20