我想将dom nodelist转换为json数组并将结果发送到rest客户端:
xml的每个节点代表以下内容:
<A NAME="x" COUNT="y">
<B KEY="z1" VALUE="z2"/>
<B KEY="z3" VALUE="z4"/>
</A>
我希望我能为输出提供一个对象数组,其中每个对象如下所示:
{"NAME":"x",
"COUNT":"y",
"B": [ {"KEY": "z1, VALUE:"z2"},
{"KEY":"z3", VALUE:"z4"} ]
}
我尝试使用GSON库:
package com.a;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class Test {
private static final String XPATH = "/A/B";
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
File f = new File("C:/Users/abc/Desktop/a.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(f);
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.compile(XPATH).evaluate(xmlDocument, XPathConstants.NODESET);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonOutput = gson.toJson(nodeList);
System.out.println(jsonOutput);
}
}
但是我收到了错误
线程中的异常&#34; main&#34;
中的java.lang.StackOverflowErrorjava.lang.StringBuffer.append(StringBuffer.java:224)
java.io.StringWriter.write(StringWriter.java:84)
go.google.gson.stream.JsonWriter.newline(JsonWriter.java:569)
com.google.gson.stream.JsonWriter.beforeName(JsonWriter.java:586)
如何修复此代码?
因为可以将整个xml转换为json(Quickest way to convert XML to JSON in Java)
我假设可以将dom节点转换为json。这有什么不对?
答案 0 :(得分:3)
出了什么问题?您只是不使用链接(Quickest way to convert XML to JSON in Java)
中描述的libGson将使用java反射从任何对象生成json字符串。从DOM Document(或节点),即使它没有以StackOverflowError结束,它也不会产生您期望的结果。这是您的XML的结果:
{"fNamespacesEnabled":false,"mutationEvents":false,"actualEncoding":"UTF-8","standalone":false,"fDocumentURI":"...a.xml","changes":0,"allowGrammarAccess":false,"errorChecking":true,"ancestorChecking":true,"xmlVersionChanged":false,"documentNumber":0,"nodeCounter":0,"xml11Version":false,"flags":6}
实际上,如果在DOM文档上调用了任何方法(例如:getDocumentElement
),gson.toJson
最终会出现StackOverflowError。
正如您在链接中所看到的,可以在此处找到可以完成工作的jar:http://mvnrepository.com/artifact/org.json/json
这意味着您将使用XPath提取的节点重新转换为字符串。 你可以这样做:
private static String toString(Node n) throws TransformerFactoryConfigurationError, TransformerException {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(n);
transformer.transform(source, result);
return result.getWriter().toString();
}
尽管如此,您所要做的就是遍历nodeList,将其转换为字符串,然后将其转换为json
for (int i = 0; i < nodeList.getLength(); i++) {
Node n = nodeList.item(i);
JSONObject xmlJSONObj = XML.toJSONObject(toString(n));
String jsonPrettyPrintString = xmlJSONObj.toString(1);
System.out.println(jsonPrettyPrintString);
}