当前位置: 首页 > 知识库问答 >
问题:

java将xml字符串转换为列表

朱鸿畅
2023-03-14

我想将一个XML字符串转换为List。

逻辑需要通用。只应将记录的XPath作为输入。有时,这可以是任何类型的数据。

我尝试了internet帮助,但由于不熟悉XML解析,无法获得通用解决方案

输入字符串

<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>

需要输出列表

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>

.
.
.
.

共有1个答案

汲昊空
2023-03-14

您可以将字符串转换为xml文档并提取节点

    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));
    }

这种方法进行所有转换,您将获得所需的输出

    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;
    }
 类似资料:
  • JavaScript具有 Java有这样的东西吗?我知道我可以用StringBuilder自己来添油加醋: ...但是如果类似的东西已经是JDK的一部分,那么这样做就没有意义了。

  • 问题内容: 是的,是的,我知道有关此主题的问题很多。但是我仍然找不到解决我问题的方法。我有一个带属性的Java对象。例如客户,如本例所示。我想要它的字符串表示形式。为此,Google建议使用JAXB。但是在所有示例中,创建的XML文件都会打印到文件或控制台,如下所示: 但是我必须使用该对象并以XML格式通过网络发送。所以我想得到一个表示XML的字符串。 我怎样才能做到这一点? 问题答案: 您可以使

  • 问题内容: 我想将包含的字符串转换为字符列表和字符哈希集。如何在Java中做到这一点? 问题答案: 您将不得不使用循环,或创建一个像这样的集合包装器,该包装器可用于原始char数组(或直接用于字符串)。 这是一个类似字符串的包装器: 不过,这是一个不变的列表。如果您想要一个可变的列表,请使用: 与此类似,您可以为其他原始类型实现此功能。 请注意,通常不建议使用此功能,因为对于每次访问,您都将进行装

  • 本文向大家介绍在Java中将字符列表转换为字符串,包括了在Java中将字符列表转换为字符串的使用技巧和注意事项,需要的朋友参考一下 假设以下是我们的字符列表- 将字符列表转换为字符串- 示例 以下是在Java中将字符列表转换为字符串的程序- 输出结果

  • 我将XML作为字符串传递给一个方法,并再次将其转换为XML来完成我的工作。 其正常工作正常,但当出现特殊字符时,如<代码> 我的XML字符串: 我的代码是: 错误: “=”是意外标记。预期标记为“;”。第1行,位置150。 完全错误为: 系统Xml。XmlException未由用户代码处理HResult=-2146232000消息=“=”是意外令牌。预期标记为“;”。第1行,位置150。源=系统。

  • 问题内容: 我有一个清单 我想将其转换为字典 我该怎么做? 问题答案: 采用: 返回: 如果需要整数,请在以下位置更改分配: 这将给出: