سؤال

وأنا أحاول تحليل تلك الوثيقة مع 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>

وقرأت كل الصفات جيدا، باستثناء "initialstate" و "اسم" ... أحصل على سمات مع معالج startElement، ولكن حجم قائمة سمة scxml هو صفر. لماذا ا؟ كيف يمكنني التغلب على هذه المشكلة؟

تعديل : ل

public void startElement(String uri, String localName, String qName, Attributes attributes){
  System.out.println(attributes.getValue("initialstate"));
  System.out.println(attributes.getValue("name")); 
}

وذلك عند تحليل العلامة الأولى، لا يعمل (يطبع "لاغية" مرتين). في الواقع،

attributes.getLength();

ويقيم إلى الصفر.

والشكر

هل كانت مفيدة؟

المحلول

ولقد حصلت على مثال كامل العمل من هناك وتكييفه لملف:

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

و

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 + ">");
    }
}

والناتج هو:

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

نصائح أخرى

وAttributes.getValue() ليس بهذه البساطة كما تبدو. في جافادوك يقول:

<اقتباس فقرة>   

وابحث عن قيمة سمة من خلال XML   المؤهلين (مسبوقة) الاسم.

وهكذا تمر في مجرد "initialstate" قد لا تعمل إذا كان هناك أي مضاعفات مساحة الاسم، لأن "initialstate" ليس من الناحية الفنية اسم مؤهل.

وأقترح وجود اللعب مع الطرق الأخرى على الطبقة Attributes، مثل getValue(int)، قد يكون لديك المزيد من خلافه مع هؤلاء.


تعديل : لالاحتمال الآخر هو أن هذا الاحتجاج startElement لا يشير إلى العنصر الذي أعتقد أنه هو. هل التحقق من أن حجة localName هو في الواقع scxml، وليس شيئا آخر؟

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top