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

懵懂的女人 提交于 2019-12-02 01:53:00

1.Is the XML formatting correct?

Yes, however do note that a processing instruction does not have attributes -- only data. What looks like attributes are part of the data and some people call them "pseudo-attributes".

2.Is this the proper way to go about parsing processing instructions using the StAX XMLStreamReader APIs?

Yes.

3.How do I use getPITarget() and getPIData() to return multiple arguments?

If by "multiple arguments" you mean the possibly more than one pseudo-attributes contained in the data, the answer is that your code must parse the data (using some standard string methods as the C# split(), and retrieve the set of name-value pairs for all pseudo-attributes.

I think that this notion of processing instructions having attributes comes from some old xml manuals. At one point there was discussion of recommending PIs to honor or require such structuring. However, the official xml specification has never mandated or even recommended such usage.

So basically you do have to parse contents yourself -- they may be in any format, but if you do know that it uses attribute notation you can parse it.

As far as I know, none of Java xml parsers or processing packages support such usage, unfortunately.

Dave Jarvis

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:

<?xml-stylesheet type="text/xsl" href="markdown.xsl"?>

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" ) );
  }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!