【问题标题】:Why is my variable is null?为什么我的变量为空?
【发布时间】:2012-12-23 14:44:28
【问题描述】:

我这样做

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

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

【问题讨论】:

  • 发这么长的cmets请把它删掉。
  • 也请只留下相关代码。
  • 您正在获取名为 Nature 的标签的内容。你能把你的 XML 也给我们看看吗?
  • 如果您只发布相关代码会有所帮助。如果您发布使用 GetNatureSax 的代码也会有所帮助。
  • 顺便说一句,仅打印相关代码不仅对我们有帮助——很有可能在找出相关代码是什么的过程中,您将解决问题自己。

标签: java xml parsing


【解决方案1】:

因为你实例化了一个新的GetNatureSax 并把它交给SaxReader 有一个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());

【讨论】:

    【解决方案2】:

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

      saxReader.setContentHandler(new GetNatureSax());
    

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

    改成

     saxReader.setContentHandler(this);
    

    解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多