Não consigo ler alguns atributos com sax
-
20-09-2019 - |
Pergunta
Estou tentando analisar esse documento com o 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>
Eu li todos os atributos bem, exceto "InitialState" e "Name" ... eu recebo os atributos com o manipulador de startElement, mas o tamanho da lista de atributos do SCXML é zero. Por quê? Como posso superar esse 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"));
}
Isso, ao analisar a primeira tag, não funciona (imprime "nulo" duas vezes). Na verdade,
attributes.getLength();
Avalia para zero.
Obrigado
Solução
Eu tenho um exemplo completo trabalhando de lá e adaptou -o para o seu arquivo:
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 + ">");
}
}
A saída é:
<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>
Outras dicas
Attributes.getValue()
não é tão simples quanto parece. o Javadoc diz:
Procure o valor de um atributo pelo nome qualificado (prefixo).
Portanto, passar apenas o "InitialState" pode não funcionar se houver alguma complicações no espaço para nome, pois o "InitialState" não é tecnicamente um nome qualificado.
Eu sugiro jogar com os outros métodos no Attributes
classe, como getValue(int)
, você pode ter mais sucesso com eles.
editar: Outra possibilidade é que essa invocação de startElement
Não está se referindo ao elemento que você acha que é. Você verificou que o localName
O argumento é de fato scxml
, e não outra coisa?