Java 在 DocumentBuilder 解析方法中使用字符串(使用 XPath 解析 XML 时需要它)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2825872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a string inside the DocumentBuilder parse method (need it for parsing XML using XPath)
提问by dierre
I'm trying to create a RESTful webservice using a Java Servlet. The problem is I have to pass via POST method to a webserver a request. The content of this request is not a parameter but the body itself.
我正在尝试使用 Java Servlet 创建 RESTful web 服务。问题是我必须通过 POST 方法将请求传递给网络服务器。这个请求的内容不是参数而是主体本身。
So I basically send from ruby something like this:
所以我基本上从 ruby 发送这样的东西:
url = URI.parse(@host)
req = Net::HTTP::Post.new('/WebService/WebServiceServlet')
req['Content-Type'] = "text/xml"
# req.basic_auth 'account', 'password'
req.body = data
response = Net::HTTP.start(url.host, url.port){ |http| puts http.request(req).body }
Then I have to retrieve the body of this request in my servlet. I use the classic readline, so I have a string. The problem is when I have to parse it as XML:
然后我必须在我的 servlet 中检索这个请求的主体。我使用经典的 readline,所以我有一个字符串。问题是当我必须将其解析为 XML 时:
private void useXML( final String soft, final PrintWriter out) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, FileNotFoundException {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(soft);
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//software/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
out.println(nodes.item(i).getNodeValue());
}
}
The problem is that builder.parse()
accepts: parse(File f)
, parse(InputSource is)
, parse(InputStream is)
.
问题是builder.parse()
接受:parse(File f)
, parse(InputSource is)
, parse(InputStream is)
。
Is there any way I can transform my xml string in an InputSource or something like that? I know it could be a dummy question but Java is not my thing, I'm forced to use it and I'm not very skilled.
有什么办法可以在 InputSource 或类似的东西中转换我的 xml 字符串吗?我知道这可能是一个愚蠢的问题,但 Java 不是我的菜,我被迫使用它而且我不是很熟练。
采纳答案by Don Roby
You can create an InputSource from a string by way of a StringReader:
您可以通过 StringReader 从字符串创建 InputSource:
Document doc = builder.parse(new InputSource(new StringReader(soft)));
回答by Istao
With your string, use something like :
使用您的字符串,使用以下内容:
ByteArrayInputStream input =
new ByteArrayInputStream(yourString.getBytes(perhapsEncoding));
builder.parse(input);
ByteArrayInputStreamis an InputStream.
ByteArrayInputStream是一个 InputStream。