4

そのドキュメントを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>

「initialstate」と「name」を除くすべての属性をよく読みました... 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")); 
}

これは、最初のタグを解析するときに機能しません (「null」が 2 回出力されます)。実際には、

attributes.getLength();

ゼロに評価されます。

ありがとう

4

2 に答える 2

3

そこから作業する完全な例があり、ファイルに合わせて調整しました:

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>
于 2010-01-13T11:58:09.593 に答える
1

Attributes.getValue()見た目ほど単純ではありません。javadocは次のように述べています。

XML 修飾 (プレフィックス) 名で属性の値を検索します。

そのため、「initialstate」は技術的には修飾名ではないため、名前空間に複雑な問題がある場合、「initialstate」だけを渡すと機能しない可能性があります。

Attributesなど、クラスの他のメソッドをgetValue(int)試してみることをお勧めします。それらでより多くの成功が得られる可能性があります。


edit : 別の可能性は、この の呼び出しがstartElement、あなたが考えている要素を参照していないことです。localName引数が実際に であり、他のものではないことを確認しましたscxmlか?

于 2010-01-13T11:44:40.493 に答える