Domanda

Sto cercando di analizzare il documento con 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>

Ho letto tutti gli attributi bene, tranne "InitialState" e "nome" ... Ottengo gli attributi con il gestore startElement, ma la dimensione della lista di attributi per SCXML è pari a zero. Perché? Come posso superare questo problema?

Modifica :

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

che, durante l'analisi del primo tag, non funziona (stampe "nulli" due volte). In realtà,

attributes.getLength();

valuta a zero.

Grazie

È stato utile?

Soluzione

Ho un esempio completo di lavoro da ci e adattato per il file:

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);
    }
}

e

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 + ">");
    }
}

L'output è:

<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>

Altri suggerimenti

Attributes.getValue() non è così semplice come sembra. Il javadoc dice:

  

Cercare il valore di un attributo da XML   qualificato (prefisso) nome.

Quindi, passando in un solo "InitialState" potrebbe non funzionare se ci sono complicazioni namespace, dal momento che "InitialState" non è tecnicamente un nome qualificato.

Posso suggerire di avere un gioco con gli altri metodi sulla classe Attributes, come getValue(int), si potrebbe avere più successo con quelli.


modifica : Un'altra possibilità è che questa invocazione di startElement non si riferisce all'elemento si pensa che è. Si è verificato che l'argomento è davvero localName scxml, e non qualcos'altro?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top