Home > Enterprise >  java convert xml String to List<String>
java convert xml String to List<String>

Time:07-06

I want to convert a String which is an XML to List.

Logic needs to be generic. only XPath of records should be taken as input. sometimes this can be any kind of data.

I tried internet help but was unable to get a generic solution being new to XML parsing.

input String

<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>

<book id="bk102">
<author>Ralls, Kim</author>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book>

<book id="bk103">
<author>Corets, Eva</author>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description>
</book>

</catalog>

Output List needed

List<String> x = some logic to get all books with each book in one String;
for(i=0;i<x.length;i  ){
System.out.println("element number "  i)
System.out.println(x[i])

}

element number 0

 <book id="bk101">
    <author>Gambardella, Matthew</author>
    <publish_date>2000-10-01</publish_date>
    <description>An in-depth look at creating applications with XML.</description>
    </book>

element number 1

<book id="bk102">
    <author>Ralls, Kim</author>
    <publish_date>2000-12-16</publish_date>
    <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
    </book>

.
.
.
.

CodePudding user response:

you can transform your string to xml document and extract nodes

    String xmlString = ...;
    String nodeName = "book";
    List<String> nodeStrList = getNodeStringList(xmlString, nodeName);
    for(int i = 0; i < nodeStrList.size(); i  ){
        System.out.println("element number "  i);
        System.out.println(nodeStrList.get(i));
    }

this method make all transformations and you'll get the output you need

    public List<String> getNodeStringList(String xmlString, String nodeName) {
        List<String> nodeStrList = new ArrayList<>();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xmlString)));
            NodeList nodeList = doc.getDocumentElement().getElementsByTagName(nodeName);
            Transformer transformer = TransformerFactory.newInstance().newTransformer();

            for (int count = 0; count < nodeList.getLength(); count  ) {
                Node node = nodeList.item(count);
                StringWriter writer = new StringWriter();
                transformer.transform(new DOMSource(node), new StreamResult(writer));
                nodeStrList.add(writer.toString());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return nodeStrList;
    }
  • Related