2010-01-13 90 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