解析要在Javaservlet中使用的XMl

解析要在Javaservlet中使用的XMl,java,xml,sax,Java,Xml,Sax,我的XML如下 <result cover="http://ia.mediaimdb.com/images /M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_ CR0,0,54,74_.jpg" title="The Amazing Spider-Man(2012)"year="2012" director="Marc Webb" rating="7.5" details="http://www.imdb

我的XML如下

<result cover="http://ia.mediaimdb.com/images      
/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_
CR0,0,54,74_.jpg" title="The Amazing Spider-Man(2012)"year="2012"
director="Marc Webb" rating="7.5"
details="http://www.imdb.com/title/tt0948470"/>

<result cover="http://ia.mediaimdb.
com/images/M/MV5BMzk3MTE5MDU5NV5BMl5BanBnXkFtZTYwMjY3NTY3._V1._SX54_CR0,
0,54,74_.jpg" title="Spider-Man(2002)" year="2002"director="Sam Raimi"
rating="7.3" details="http://www.imdb.com/title/tt0145487"/>

<result cover="http://ia.mediaimdb.
com/images/M/MV5BODUwMDc5Mzc5M15BMl5BanBnXkFtZTcwNDgzOTY0MQ@@._V1._SX54_
CR0,0,54,74_.jpg" title="Spider-Man 3 (2007)" year="2007" director="Sam
Raimi" rating="6.3" details="http://www.imdb.com/title/tt0413300"/>

<result cover="http://i.mediaimdb.
com/images/SF1f0a42ee1aa08d477a576fbbf7562eed/realm/feature.gif" title="
The Amazing Spider-Man 2 (2014)" year="2014" director="Sam Raimi"
rating="6.3" details="http://www.imdb.com/title/tt1872181"/>

<result cover="http://ia.mediaimdb.
com/images/M/MV5BMjE1ODcyODYxMl5BMl5BanBnXkFtZTcwNjA1NDE3MQ@@._V1._SX54_
CR0,0,54,74_.jpg" title="Spider-Man 2 (2004)" year="2004" director="Sam
Raimi" rating="7.5" details="http://www.imdb.com/title/tt0316654"/>
</results>

我需要使用类似saxp的解析器解析这个XML文件,并将结果输出为json字符串。 我是新来的。我一直在用谷歌搜索,但我不能简单地理解它


有什么想法吗?

让我用一个简单的例子来解释

输入XML:

<Employees>
    <employee name="vels" gender="male"/>
    <employee name="steave" gender="male"/>
</Employees>
输出(手动格式化):


下面是一个简单而得体的例子。希望你能继续。

SAXP,你是说java SAXParser?是的。但是使用DOM解析器对我来说很容易理解,我已经尝试过DOM解析器了。我目前正在为它编写代码。我只是认为参考代码会有所帮助。这里有一个简单的例子。试试这个。如果你不能,我将帮助你,我目前正在用记事本++编写代码。今天早些时候,我已经读了很多教程。我的代码大约一小时后就会准备好。我将编辑我的问题并在那里发布我的代码。如果你能帮我的话。非常感谢。:-)
final JSONObject jsonObj = new JSONObject();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();

DefaultHandler handler = new DefaultHandler() {
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        try {
            if (qName.equals("Employees")) {
                // reject root node
                return;
            }
            int len = attributes.getLength();
            HashMap<String, String> map = new HashMap< String, String>();
            for (int i = 0; i < len; i++) {
                String attribName = attributes.getLocalName(i);
                String attribVal = attributes.getValue(i);
                map.put(attribName, attribVal);


            }
            jsonObj.append("employee", map);
        } catch (JSONException ex) {
            // handle excep
        }

    }
};

saxParser.parse("c:/employee.xml", handler);
String jSonOutput = jsonObj.toString();
// process this json outpout
System.out.println(jSonOutput);
   {"employee":
    [
     {"name":"vels","gender":"male"},
     {"name":"steave","gender":"male"}
    ]
   }