Pregunta

Estoy tratando de analizar ese 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>

He leído todos los atributos Bueno, excepto "nombre" "initialState" y ... Consigo los atributos con el manejador startElement, pero el tamaño de la lista de atributos para SCXML es cero. ¿Por qué? ¿Cómo puedo superar ese problema?

Editar

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

que, al analizar la primera etiqueta, no funciona (impresiones "nulo" dos veces). De hecho,

attributes.getLength();

evalúa a cero.

Gracias

¿Fue útil?

Solución

Tengo un ejemplo completo trabajando desde no y lo adaptó para el archivo:

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

y

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

La salida es:

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

Otros consejos

Attributes.getValue() no es tan simple como que se vea. La javadoc dice:

  

Busque el valor de un atributo de XML   cualificado (con el prefijo) nombre.

Así que pasa en tan sólo "initialState" podría no funcionar si hay alguna complicación de espacio de nombres, ya que "initialState" técnicamente no es un nombre calificado.

Sugiero tener una obra de teatro con los otros métodos de la clase Attributes, como getValue(int), es posible que tenga más éxito con ellos.


editar : Otra posibilidad es que esta invocación de startElement no se refiere al elemento que piensa que es. ¿Ha verificado que el argumento es, en efecto localName scxml, y no otra cosa?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top