문제

나는 그 문서를 색소폰으로 구문 분석하려고합니다.

<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"및 "Name"을 제외한 모든 속성을 잘 읽었습니다. STARLEMENT HANDLER가있는 속성을 얻지 만 SCXML의 속성 목록의 크기는 0입니다. 왜요? 그 문제를 어떻게 극복 할 수 있습니까?

편집하다:

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

첫 번째 태그를 구문 분석 할 때 작동하지 않습니다 ( "NULL"을 두 번 인쇄합니다). 사실로,

attributes.getLength();

0으로 평가합니다.

감사

도움이 되었습니까?

해결책

나는 전체 예제가있다 거기 파일에 적합했습니다.

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() 모양만큼 간단하지 않습니다. 그만큼 Javadoc 말 :

XML 자격 (접두사) 이름으로 속성의 값을 찾으십시오.

따라서 "InitialState"가 기술적으로 자격을 갖춘 이름이 아니기 때문에 네임 스페이스 합병증이 있으면 "Initialstate"만 통과하는 것이 작동하지 않을 수 있습니다.

나는 다른 방법으로 놀이를하는 것이 좋습니다. Attributes 다음과 같은 클래스 getValue(int), 당신은 그것들과 더 많은 성공을 가질 수 있습니다.


편집하다: 또 다른 가능성은이 호출입니다 startElement 당신이 생각하는 요소를 언급하지 않습니다. 당신은 그것을 확인 했습니까? localName 논쟁은 참으로입니다 scxml, 다른 것이 아닌가?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top