私はこれをします
package file;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.LocatorImpl;
import org.xml.sax.helpers.XMLReaderFactory;
public class GetNatureSax implements ContentHandler {
private final static Logger _logger = Logger.getLogger(GetNatureSax.class.getName());
private boolean isCurrentElement = false;
private Locator locator;
private String _parameterValue = null;
public GetNatureSax() {
super();
locator = new LocatorImpl();
}
public String getValeurParametre() {
return _parameterValue;
}
public void setDocumentLocator(Locator value) {
locator = value;
}
public void startDocument() throws SAXException {}
public void endDocument() throws SAXException {}
public void startPrefixMapping(String prefix, String URI) throws SAXException {
}
public void endPrefixMapping(String prefix) throws SAXException {}
public void startElement(String nameSpaceURI, String localName, String rawName, Attributes attributs)
throws SAXException {
if (localName.equals("Nature")) {
isCurrentElement = true;
}
}
public void endElement(String nameSpaceURI, String localName, String rawName) throws SAXException {
if (localName.equals("Nature")) {
isCurrentElement = false;
}
}
public void characters(char[] ch, int start, int end) throws SAXException {
if (isCurrentElement) {
_parameterValue = new String(ch, start, end);
}
}
public void ignorableWhitespace(char[] ch, int start, int end) throws SAXException {
System.out.println("espaces inutiles rencontres : ..." + new String(ch, start, end) + "...");
}
public void processingInstruction(String target, String data) throws SAXException {
System.out.println("Instruction de fonctionnement : " + target);
System.out.println(" dont les arguments sont : " + data);
}
public void skippedEntity(String arg0) throws SAXException {}
public void parseFichier(String i_fichierATraiter) {
XMLReader saxReader;
try {
saxReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
saxReader.setContentHandler(new GetNatureSax());
saxReader.parse(i_fichierATraiter);
System.out.println(_parameterValue);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
私のXML:
...
<Reference>
<Nature>ACHAT</Nature>
<Statut>2</Statut>
<Type-Gest>RE</Type-Gest>
<Gest>RE_ELECTRA</Gest>
<Type-Res>D</Type-Res>
<Nb-h>24</Nb-h>
</Reference>
...
なぜこの行を実行すると
System.out.println(_parameterValue);
私の変数はnullで、デバッグする前に私の変数はnullではありません