Ne peut pas lire certains attributs avec SAX
-
20-09-2019 - |
Question
Je suis en train d'analyser ce document avec 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>
Je lis tous les attributs bien, sauf « InitialState » et « name » ... Je reçois les attributs avec le gestionnaire startElement, mais la taille de la liste d'attributs pour SCXML est nul. Pourquoi? Comment puis-je surmonter ce problème?
Modifier :
public void startElement(String uri, String localName, String qName, Attributes attributes){
System.out.println(attributes.getValue("initialstate"));
System.out.println(attributes.getValue("name"));
}
que, lors de l'analyse de la première étiquette, ne fonctionne pas (affiche « null » deux fois). En fait,
attributes.getLength();
évalue à zéro.
Merci
La solution
J'ai un exemple complet de travail il et adapté pour votre fichier:
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);
}
}
et
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 sortie est la suivante:
<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>
Autres conseils
Attributes.getValue()
n'est pas aussi simple que regarder. javadoc dit:
Recherchez la valeur d'un attribut par XML nom qualifié (préfixés).
passant juste « InitialState » pourrait ne pas fonctionner s'il y a des complications d'espace de noms, car « InitialState » est pas techniquement un nom qualifié.
Je suggère d'avoir un jeu avec les autres méthodes de la classe de Attributes
, comme getValue(int)
, vous pourriez avoir plus avec les succès.
modifier : Une autre possibilité est que cette invocation de startElement
ne fait pas référence à l'élément que vous pensez qu'il est. Avez-vous vérifié que l'argument est en effet localName
scxml
, et pas autre chose?