-2

我这样做

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

我的变量为空,在我调试之前我的变量不为空

4

2 回答 2

1

因为你实例化了一个 newGetNatureSax并将它交给了SaxReaderhas a Content Handler

因此,当解析结束时,是新GetNatureSax实例已被修改并_parameterValue设置了字段,而不是当前实例(this)

只需像这样修改您的parseFichier方法:

saxReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
saxReader.setContentHandler(this);  // here
saxReader.parse(i_fichierATraiter);
System.out.println("Found: " + getValeurParametre());
于 2013-01-09T14:28:24.670 回答
1

使用我的调试器,我可以看到您正在使用两个 GetNatureSax

  saxReader.setContentHandler(new GetNatureSax());

这将创建第二个 GetNatureSax 对象,在该对象中设置值。

将其更改为

 saxReader.setContentHandler(this);

解决了这个问题。

于 2013-01-09T14:31:46.493 回答