# Get single vaue from xml by Java

**URL:** <https://discuss.elastic.co/t/get-single-vaue-from-xml-by-java/46237>\
**Category:** Elasticsearch\
**Created:** [April 4, 2016, 12:35pm UTC](https://discuss.elastic.co/t/get-single-vaue-from-xml-by-java/46237 "2016-04-04T12:35:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![agent0009](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@agent0009](https://discuss.elastic.co/u/agent0009)\
**Post date:** [April 4, 2016, 12:35pm UTC](https://discuss.elastic.co/t/get-single-vaue-from-xml-by-java/46237/1 "2016-04-04T12:35:59Z")

</div>

Hi Guys,

I'm new to Elasticsearch and i'm trying to get a single field value with an ID.

> LOG.info("Starte Client für Verbindung zum Elasticsearch-Server:");  
> Client client = TransportClient.builder().build()  
> .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

> ```
> GetResponse getResponse = client.prepareGet("database", "xsd", xsdid).execute().actionGet();
> 
> ```

> ```
> LOG.info("Erhalte Antwort und Ergebnis:");
> Map<String, Object> source = getResponse.getSource();
> 
> ```

this gets me the full file back but i just want the field **\< xmldata\>.... \</ xmldata\>** back.  
can someone help me?

---

<div class="post-metadata">

**Author:** ![abeyad](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@abeyad](https://discuss.elastic.co/u/abeyad)\
**Post date:** [April 4, 2016, 5:37pm UTC](https://discuss.elastic.co/t/get-single-vaue-from-xml-by-java/46237/2 "2016-04-04T17:37:06Z")

</div>

You can specify which fields you want returned in the request by doing something like:

`client.prepareGet("database", "xsd", xsdid).setFields("xmldata")`

You can also disable fetching the source by adding `setFetchSource(false)`

---

<div class="post-metadata">

**Author:** ![agent0009](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@agent0009](https://discuss.elastic.co/u/agent0009)\
**Post date:** [April 5, 2016, 6:17am UTC](https://discuss.elastic.co/t/get-single-vaue-from-xml-by-java/46237/3 "2016-04-05T06:17:45Z")

</div>

> [@abeyad](#):
>
> setFetchSource(false)

Thanks for the response. So now i'm not csure how to process the xmldata after setting xmldata:  
I added your part:

> `GetResponse getResponse = client.prepareGet("database", "xsd", xsdid).setFetchSource(false).setFields("xmldata").execute().actionGet();`

> ```
> LOG.info("Erhalte Antwort und Ergebnis:");
> Map<String, Object> source = getResponse.getSource();
> 
> ```

> ```
> This has to be changed! Map<String, Object> xmldata = getResponse.getField("xmldata");
> 
> ```

Can you help me?

---

<div class="post-metadata">

**Author:** ![abeyad](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@abeyad](https://discuss.elastic.co/u/abeyad)\
**Post date:** [April 5, 2016, 1:29pm UTC](https://discuss.elastic.co/t/get-single-vaue-from-xml-by-java/46237/4 "2016-04-05T13:29:56Z")

</div>

`getResponse.getField("xmldata").getValues()` which will return a list of values.. presume this is what you want? I don't know what your documents look like either, so I'm not even sure if you have the "xmldata" field or if its just part of xml that is part of a larger single string field.

---

<div class="post-metadata">

**Author:** ![agent0009](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@agent0009](https://discuss.elastic.co/u/agent0009)\
**Post date:** [April 5, 2016, 2:09pm UTC](https://discuss.elastic.co/t/get-single-vaue-from-xml-by-java/46237/5 "2016-04-05T14:09:54Z")

</div>

> [@abeyad](#):
>
> getResponse.getField("xmldata").getValues()

I want something like you wrote.  
Here is my .xml

```
> <?xml version="1.0" encoding="UTF-8"?><XmlDoc><FunctionResult result="success"><Message>Processed Correct!</Message></FunctionResult>
> <billing>
> <billing_id>569</billing_id>
> <appid>mft</appid>
> <senderbtid>20041900-107</senderbtid>
> <receiverbtid>20041900-40</receiverbtid>
> <messagetype>M_ORDERS</messagetype>
> <messageid>15336589</messageid>
> <messageprops/>
> <tmid>841</tmid>
> <tmmsgcnt>0</tmmsgcnt>
> <tmmsgtotal>1</tmmsgtotal>
> <procdate>2014-12-01T01:00:20.405</procdate>
> <xmldata>PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48WG1sRG9jPg0KPGJpbGxpbmc+DQo8YmlsbGluZ19pZD41Njk8L2JpbGxpbmdfaWQ+DQo8YXBwaWQ+bWZ0PC9hcHBpZD4NCjxzZW5kZXJidGlkPjIwMDQxOTAwLTEwNzwvc2VuZGVyYnRpZD4NCjxyZWNlaXZlcmJ0aWQ+MjAwNDE5MDAtNDA8L3JlY2VpdmVyYnRpZD4NCjxtZXNzYWdldHlwZT5NX09SREVSUzwvbWVzc2FnZXR5cGU+DQo8bWVzc2FnZWlkPjE1MzM2NTg5PC9tZXNzYWdlaWQ+DQo8bWVzc2FnZXByb3BzLz4NCjx0bWlkPjg0MTwvdG1pZD4NCjx0bW1zZ2NudD4wPC90bW1zZ2NudD4NCjx0bW1zZ3RvdGFsPjE8L3RtbXNndG90YWw+DQo8cHJvY2RhdGU+MjAxNC0xMi0wMVQwMTowMDoyMC40MDU8L3Byb2NkYXRlPg0KPC9iaWxsaW5nPg0KPC9YbWxEb2M+DQo=</xmldata></billing>
> </XmlDoc>

```

And now i just want the xml-part here. I've tried many things but everytime i get the NullPointer - Error  
LOG.info("Starte Client für Verbindung zum Elasticsearch-Server:");  
Client client = TransportClient.builder().build()  
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

```
        GetResponse getResponse = client.prepareGet("database", "xsd", xsdid).execute().actionGet();
        
        System.out.println("xmldata: " + getResponse.getField("xmldata").getValues());

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:02pm UTC](https://discuss.elastic.co/t/get-single-vaue-from-xml-by-java/46237/6 "2017-07-05T23:02:16Z")

</div>


