如何使用java解析多级标签和CDATA的XML文件?

时间:2014-11-07 04:46:16

标签: java xml-parsing cdata

我想通过Java使用多级标签和CDATA读取XML文件。 示例XML是:

<?xml version="1.0" encoding="UTF-8"?>
<Result>
  <ResultDetails>
  <SearchFilmResult ItemType="film">
 <FilmDetails>
    <FilmDetail>
       <Film Code="INCEPTION"><![CDATA[INCEPTION 2010]]></Film>
       <Imdb>8.8</Imdb>
       <FilmInformation>
            <Director><![CDATA[Christopher Nolan]]></Director> 
            <Actors>
                <Actor1><![CDATA[Leonardo DiCaprio]]></Actor1>
                <Actor2><![CDATA[Joseph Gordon-Levitt]]></Actor2>
                <Actor3><![CDATA[Ellen Page]]></Actor3>
            </Actors>  
       </FilmInformation>
    </FilmDetail>
   </FilmDetails>
  </SearchFilmResult>
  </ResultDetails>
</Result>

预期结果是:

  

电影代码= INCEPTION

     

电影名称= INCEPTION 2010

     

IMDB = 8.8

     

导演=克里斯托弗·诺兰

     

Actors = Leonardo DiCaprio,Joseph Gordon-Levitt,Joseph Gordon-Levitt

有没有人可以指导我该怎么办?非常感谢。

1 个答案:

答案 0 :(得分:0)

你看过XPath了吗?

这是一个非常简单的例子,它将解析这个示例XML,但我认为你应该去探索那里的可能性,并确定什么对你有用。

尝试一下:

public class Test {

    public static void main(String[] args) throws Exception {
        // sample xml
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Result>\n" +
                "  <ResultDetails>\n" +
                "  <SearchFilmResult ItemType=\"film\">\n" +
                " <FilmDetails>\n" +
                "    <FilmDetail>\n" +
                "       <Film Code=\"INCEPTION\"><![CDATA[INCEPTION 2010]]></Film>\n" +
                "       <Imdb>8.8</Imdb>\n" +
                "       <FilmInformation>\n" +
                "            <Director><![CDATA[Christopher Nolan]]></Director> \n" +
                "            <Actors>\n" +
                "                <Actor1><![CDATA[Leonardo DiCaprio]]></Actor1>\n" +
                "                <Actor2><![CDATA[Joseph Gordon-Levitt]]></Actor2>\n" +
                "                <Actor3><![CDATA[Ellen Page]]></Actor3>\n" +
                "            </Actors>  \n" +
                "       </FilmInformation>\n" +
                "    </FilmDetail>\n" +
                "   </FilmDetails>\n" +
                "  </SearchFilmResult>\n" +
                "  </ResultDetails>\n" +
                "</Result>";

        // read the xml
        InputSource source = new InputSource(new StringReader(xml));

        // build a document model
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(source);

        // create an xpath interpreter
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();

        // evaluate nodes
        String filmCode = xpath.evaluate("Result/ResultDetails/SearchFilmResult/FilmDetails/FilmDetail/Film/@Code", document);
        String filmName = xpath.evaluate("Result/ResultDetails/SearchFilmResult/FilmDetails/FilmDetail/Film", document);
        String imdb = xpath.evaluate("Result/ResultDetails/SearchFilmResult/FilmDetails/FilmDetail/Imdb", document);
        String director = xpath.evaluate("Result/ResultDetails/SearchFilmResult/FilmDetails/FilmDetail/FilmInformation/Director", document);

        // get actor data
        XPathExpression expr = xpath.compile("Result/ResultDetails/SearchFilmResult/FilmDetails/FilmDetail/FilmInformation/Actors/child::*");
        NodeList actors = (NodeList) expr.evaluate(document, XPathConstants.NODESET);

        StringBuilder sb = new StringBuilder();

        // compile actor list
        for ( int i = 0; i < actors.getLength(); i++ ) {
            String actorName = actors.item(i).getFirstChild().getNodeValue();
            if ( i > 0 ) {
                sb.append(", ");
            }
            sb.append(actorName);
        }

        // print output
        System.out.println("Film Code = " + filmCode);
        System.out.println("Film Name = " + filmName);
        System.out.println("IMDB = " + imdb);
        System.out.println("Director = " + director);
        System.out.println("Actors = " + sb.toString());
    }
}

输出:

Film Code = INCEPTION
Film Name = INCEPTION 2010
IMDB = 8.8
Director = Christopher Nolan
Actors = Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen Page