首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打印Web服务响应

打印Web服务响应
EN

Stack Overflow用户
提问于 2012-11-30 18:03:42
回答 2查看 480关注 0票数 1

我正在尝试调用一个and服务并打印一些响应。

当我运行这段代码时,我得到了带有ID、FIRSTNAME、LASTNAME、STREET、CITY的XML响应。那么,例如,我如何才能只打印出城市?

代码语言:javascript
复制
static int customerId = 123456;
    public static void main(String[] args) throws Exception {


        URL oracle = new URL(
                "http://www.thomas-bayer.com/sqlrest/CUSTOMER/" + customerId);
        BufferedReader in = new BufferedReader(new InputStreamReader(
                oracle.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        in.close();

    }

提前谢谢你。

EN

回答 2

Stack Overflow用户

发布于 2013-01-27 20:24:51

这可能是一个微调的代码,但仍然可以。

代码语言:javascript
复制
static int customerId = 123456; 

static String str="";

public static void main(String[] args) throws Exception 
{

    URL oracle        = new URL("http://www.thomas-bayer.com/sqlrest/CUSTOMER/" + customerId);
    BufferedReader in = new BufferedReader(new InputStreamReader(
            oracle.openStream()));

    String inputLine;

    while ((inputLine = in.readLine()) != null){
            System.out.println(inputLine);
        //code change stats here
            if(inputLine.contains("<CITY>")){
                str=inputLine;
            }
        }
        String city=str.replace("<CITY>","");
       System.out.println(city.replace("</CITY>", ""));
       //code change ends here
    in.close();

}
票数 0
EN

Stack Overflow用户

发布于 2013-01-27 20:36:03

这应该是最好的一个:

通过传递键和字符串,在while循环中调用此方法:

代码语言:javascript
复制
public static String getvalue(String xmlkey,String xmlstring) throws
 ParserConfigurationException, SAXException, IOException{
        
    System.out.println(xmlstring+"dff");
    InputStream is = new ByteArrayInputStream(xmlstring.getBytes());
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;
    try {
        db = dbf.newDocumentBuilder();
        org.w3c.dom.Document doc = null;
        doc = db.parse(is);
        NodeList nl = doc.getElementsByTagName(xmlkey);
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                Node item = nl.item(i);
                String name = item.getNodeName();
                String value = item.getTextContent();
                System.out.println(name+" "+value+" value and name");
            }
        }
        return value;
    } catch(Exception e) {
        e.printStackTrace();
    }
                    
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13642929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档