3

私はこのようなxmlを持っています:

<Message xmlns="uri_of_message">
  <VendorId>1234</VendorId>
  <SequenceNumber>1</SequenceNumber>
  ...other important headers...
  <Data>
    <Functions xmlns="uri_of_functions_subxml">
      <Function1 attr="sth">
        <Info>Some_Info</Info>
      </Function1>
      <Function2>
        <Info>Some_Info</Info>
      </Function2>
      ...Functions n...
    </Functions>
  </Data>
</Message>

内部xmlを抽出する必要があります

<Functions xmlns="uri_of_functions_subxml">
  <Function1 attr="sth">
    <Info>Some_Info</Info>
  </Function1>
  <Function2>
    <Info>Some_Info</Info>
  </Function2>
  ...Functions n...
</Functions>

私は最初に文字メソッドで内部xmlを取得しようとしました:

 public void startElement(String uri, String localName, String tagName, Attributes attributes) throws SAXException {
if (tagName.equalsIgnoreCase("Data")){
  buffer = new StringBuffer();}
}
public void characters(char[] ch, int start, int length) throws SAXException {
  if (buffer != null) {
     buffer.append(new String(ch, start, length).trim());
  }
}
public void endElement(String uri, String localName, String tagName) throws SAXException {
if (tagName.equalsIgnoreCase("Data")){
innerXML = buffer.toString().trim();
}

しかし、文字メソッドが xml を適切に収集していないことに気付きました。「<」、「>」などの特殊文字を拒否した可能性があります。

以下のリンクには同じ質問が含まれていますが、外側のxmlは一種のハンドシェイク信号として処理する必要があり、内側のxmlはまったく異なる方法で処理する必要があるため、答えは私には当てはまりません.

Java XML 解析: SAX を使用した内部 XML の取得

私が必要とするのは、内部の xml を適切に収集することだけです。しかし、それを行う方法は?前もって感謝します..

4

1 に答える 1

5

SAX はこの仕事に最適な選択ではないようですが、とにかく試してみてください

    SAXParser p = SAXParserFactory.newInstance().newSAXParser();
    XMLReader filter = new XMLFilterImpl(p.getXMLReader()) {
        private boolean inFunctions;

        @Override
        public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
            if (!inFunctions && qName.equals("Functions")) {
                inFunctions = true;
            }
            if (inFunctions) {
                super.startElement(uri, localName, qName, atts);
            } else {
                qName.equals("Functions");
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if (inFunctions) {
                super.endElement(uri, localName, qName);
                if (qName.equals("Functions")) {
                    inFunctions = false;
                }
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (inFunctions) {
                super.characters(ch, start, length);
            }
        }
    };
    Transformer t = TransformerFactory.newInstance().newTransformer();
    Source source = new SAXSource(filter, new InputSource(new FileInputStream("1.xml")));
    Result result = new StreamResult(System.out);
    t.transform(source, result);
}

出力

<?xml version="1.0" encoding="UTF-8"?><Functions xmlns="uri_of_functions_subxml">
      <Function1 attr="sth">
        <Info>Some_Info</Info>
      </Function1>
      <Function2>
        <Info>Some_Info</Info>
      </Function2>
    </Functions>

公式チュートリアル

于 2013-01-17T09:24:45.847 に答える