简体   繁体   English

Java:使用SAX解析xml文件

[英]Java: parse xml file using SAX

I have this xml data to parse using SAX. 我有此XML数据要使用SAX进行解析。 The problem is that I cannot figure out how to get the data from it. 问题是我无法弄清楚如何从中获取数据。 The most important thing to get from it is the encoded data (fileContent) which I believe is base64. 从中获得的最重要的信息是我认为是base64的编码数据(fileContent)。 What I need to do with that is to make an Excel .xls file from it. 我需要做的是从中制作一个Excel .xls文件。 I have tried some things, but I can only get some field/node (eg refNumber, userEmail, etc.) names, but not their actual value. 我已经尝试了一些方法,但是只能得到一些字段/节点(例如refNumber,userEmail等)名称,而不能得到它们的实际值。 I have placed some code snippets below. 我在下面放置了一些代码片段。 Could anyone please help me? 谁能帮我吗?

Thanks! 谢谢!

class SomeClass {
...
private String currentElement;
...
public Result parseSerializedData(String serializedData) throws SAXException, TransformerConfigurationException, TransformerException
    {
        System.out.println("-------------------");
        System.out.println("Serialized: " + serializedData);
        Source src = new SAXSource(xr, new InputSource(new StringReader(serializedData)));
        Result res = new StreamResult(System.out);
        System.out.println("Res 1:" + res);

        TransformerFactory.newInstance().newTransformer().transform(src, res);
        System.out.println("transform 1:" + res);

        try {
         SAXParserFactory factory = SAXParserFactory.newInstance();
         SAXParser saxParser = factory.newSAXParser();
         saxParser.parse(serializedData, new MyHandler());
          } catch (Exception e) {
             e.printStackTrace();
          }

        System.out.println("The current element is: " + currentElement);
        System.out.println("-------------------");
        return res;
    }

    /*
    * Inner class for the Callback Handlers.
    */
   class MyHandler extends DefaultHandler {
      // Callback to handle element start tag
      @Override
      public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
          System.out.println("qName: " + qName);
         currentElement = qName;
      }

      // Callback to handle element end tag
      @Override
      public void endElement(String uri, String localName, String qName)
            throws SAXException {
         currentElement = "";
      }

      // Callback to handle the character text data inside an element
      @Override
      public void characters(char[] chars, int start, int length) throws SAXException {
        BASE64Decoder decoder = new BASE64Decoder();
          try {
            byte[] decodedBytes = decoder.decodeBuffer(String.valueOf(chars));
              System.out.println("The current element2 is: " + currentElement);
              if (currentElement.equals("fileContent")) {
                System.out.println("\tfileContent:\t" + new String(decodedBytes, start, length));
             }
          } catch (IOException e) {
              e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
          }

      }
   }
}

serializedData is the contents of that xml file serializedData是该xml文件的内容

Basically the characters method is where the values are read. 基本上,使用characters方法读取值。 In your case you were printing only for one tag. 在您的情况下,您仅打印一个标签。 if (currentElement.equals("fileContent")) . if (currentElement.equals("fileContent")) Follow the below program. 请遵循以下程序。 This displays all values of all tags. 这将显示所有标签的所有值。 Another thing to notice is the characters method reads a chuck of max 2048 bytes (if i remember correctly), so the best approach is to use append later process the value in endElement() method as shown in the example. 还要注意的另一件事是, characters方法读取最大2048个字节的卡盘(如果我没记错的话),因此最好的方法是使用append稍后处理endElement()方法中的值,如示例所示。 Please not I'm using DatatypeConverter for Base64 decoding. 请不要为Base64解码使用DatatypeConverter You could use your own decoder. 您可以使用自己的解码器。

import java.io.File;

import javax.xml.bind.DatatypeConverter;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SaxSample {

    public static void main(String argv[]) {

        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();

            DefaultHandler handler = new DefaultHandler() {

                StringBuilder value;

                public void startElement(String uri, String localName,
                        String qName, Attributes attributes)
                        throws SAXException {
                    value = new StringBuilder();
                }

                public void endElement(String uri, String localName,
                        String qName) throws SAXException {
                    if ("fileContent".equalsIgnoreCase(qName)) {
                        String decodedValue = new String(DatatypeConverter.parseBase64Binary(value.toString()));
                        System.out.println(qName + " = " + decodedValue);
                    } else {
                        System.out.println(qName + " = " + value);
                    }
                    value = new StringBuilder();
                }

                public void characters(char ch[], int start, int length)
                        throws SAXException {
                    value.append(new String(ch, start, length));
                }

            };

            saxParser.parse(new File("data.xml"), handler);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM