How do I format and read XML processing instructions using Java StAX?

后端 未结 3 1482
旧巷少年郎
旧巷少年郎 2021-01-21 10:53

First, how do I format the XML processing instruction, is it:


Usi

3条回答
  •  [愿得一人]
    2021-01-21 11:46

    Although Dimitre's answer is technically correct, a few popular libraries now parse processing instruction pseudo-attributes as would be expected. The subsequent examples parse the following XML processing instruction to obtain the value for the href pseduo-attribute:

    
    

    JDOM2

    Using JDOM2:

    import org.jdom2.ProcessingInstruction;
    import org.xml.sax.helpers.DefaultHandler;
    
    public class ProcessingInstructionHandler extends DefaultHandler {
    
      @Override
      public void processingInstruction( final String target, final String data ) {
        final ProcessingInstruction pi = new ProcessingInstruction( target, data );
        System.out.println( pi.getPseudoAttributeValue( "href" ) );
      }
    }
    

    Saxon

    Using Saxon:

    import static net.sf.saxon.tree.util.ProcInstParser.getPseudoAttribute;
    import org.xml.sax.helpers.DefaultHandler;
    
    public class ProcessingInstructionHandler extends DefaultHandler {
    
      @Override
      public void processingInstruction( final String target, final String data ) {
        System.out.println( getPseudoAttribute( data, "href" ) );
      }
    }
    

提交回复
热议问题