简体   繁体   English

从HTTP XML响应中提取元素-HTTP客户端和Java

[英]Extracting elements from an HTTP XML response--HTTP Client & Java

So I've gotten help from here already so I figured why not try it out again!? 所以我已经从这里得到了帮助,所以我想出了为什么不再次尝试它呢? Any suggestions would be greatly appreciated. 任何建议将不胜感激。

I'm using HTTP client and making a POST request; 我正在使用HTTP客户端并发出POST请求; the response is an XML body that looks like the following: 响应是一个如下所示的XML主体:

<?xml version="1.0" encoding="UTF-8"?>
<CartLink 
    xmlns="http://api.gsicommerce.com/schema/ews/1.0">
    <Name>vSisFfYlAPwAAAE_CPBZ3qYh</Name>
    <Uri>carts/vSisFfYlAPwAAAE_CPBZ3qYh</Uri>
</CartLink>

Now... 现在...

I have an HttpEntity which is 我有一个HttpEntity

[HttpResponse].getEntity(). 

Then I get a String representation of the response (which is XML in this case) by saying 然后,通过说出响应的字符串表示形式(在这种情况下为XML)

String content = EntityUtils.toString(HttpEntity)

I tried following some of the suggestions on this post: How to create a XML object from String in Java? 我尝试遵循这篇文章中的一些建议: 如何从Java中的String创建XML对象? but it did not seem to work for me. 但它似乎对我不起作用。 When I built up the document it still appeared to be null. 当我建立文档时,它仍然显示为空。

MY END GOAL here is just to get the NAME field.. ie the "vSisFfYlAPwAAAE_CPBZ3qYh" part. 我的最终目标只是为了获得名称字段。即“ vSisFfYlAPwAAAE_CPBZ3qYh”部分。 So do I want to build up a document and then extract it...? 那我想建立一个文件然后解压缩...吗? Or is there a simpler way? 还是有更简单的方法? I've been trying different things and I can't seem to get it to work. 我一直在尝试不同的方法,但似乎无法正常工作。

Thanks for all of the help guys, it is most appreciated!! 感谢所有帮助人员,非常感谢!!

Instead of trying to extract the value with string manipulation, try to use Java's inbuilt ability to parse XML. 与其尝试通过字符串操作来提取值,不如尝试使用Java的内置功能来解析XML。 That's a much better approach. 那是一个更好的方法。 Http Components returns responses in an XML format - there's a reason for that. Http Components以XML格式返回响应-这是有原因的。 :) :)

Here's probably one way to solve your problem: 这可能是解决问题的一种方法:

    // Parse the response using DocumentBuilder so you can get at elements easily
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(response);
    Element root = doc.getDocumentElement();
    // Now let's say you have not one, but 'n' nodes that contain the value
    // you're looking for. Use NodeList to get a list of all those nodes and just 
    // pull out the tag/attribute's value you want.
    NodeList nameNodesList = doc.getElementsByTagName("Name");
    ArrayList<String> nameValues = null;
    // Now iterate through the Nodelist to get the values you want.
    for (int i=0; i<nameNodesList.getLength(); i++){
         nameValues.add(nameNodesList.item(i).getTextContent());
    }

The ArrayList "nameValues" will now hold every single value contained within "Name" tags. ArrayList“ nameValues”现在将包含“ Name”标签中包含的每个单个值。 You could also create a HashMap to store a key value pair of Nodes and their respective text contents. 您还可以创建一个HashMap来存储节点的键值对及其各自的文本内容。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM