在DocumentBuilder解析方法中使用字符串(需要它使用XPath解析XML) [duplicate]

时间:2022-08-01 17:02:54

This question already has an answer here:

这个问题已经有了答案:

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方法向webserver传递一个请求。这个请求的内容不是参数,而是主体本身。

So I basically send from ruby something like this:

所以我基本上是这样发送的:

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

问题是build .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.

有什么方法可以将xml字符串转换成InputSource之类的?我知道这可能是一个虚拟的问题,但Java不是我喜欢的,我*使用它,而且我不是很熟练。

2 个解决方案

#1


49  

You can create an InputSource from a string by way of a StringReader:

可以通过StringReader从字符串中创建一个InputSource:

Document doc = builder.parse(new InputSource(new StringReader(soft)));

#2


3  

With your string, use something like :

使用你的字符串,使用如下内容:

ByteArrayInputStream input = 
 new ByteArrayInputStream(yourString.getBytes(perhapsEncoding));
builder.parse(input);

ByteArrayInputStream is an InputStream.

ByteArrayInputStream InputStream。

#1


49  

You can create an InputSource from a string by way of a StringReader:

可以通过StringReader从字符串中创建一个InputSource:

Document doc = builder.parse(new InputSource(new StringReader(soft)));

#2


3  

With your string, use something like :

使用你的字符串,使用如下内容:

ByteArrayInputStream input = 
 new ByteArrayInputStream(yourString.getBytes(perhapsEncoding));
builder.parse(input);

ByteArrayInputStream is an InputStream.

ByteArrayInputStream InputStream。