Jump to content

Java Help


Recommended Posts

Posted

naku oka scenario undhi....  like below

 

<tagset>
<systemTagSetName>topic</systemTagSetName>
<displayTagSetName>Topic</displayTagSetName>
<tag>
<systemTagName>9005482</systemTagName>
<displayTagName>Forgot username or password</displayTagName>
</tag
</tagset>
<tagset>
<systemTagSetName>region</systemTagSetName>
<displayTagSetName>Region</displayTagSetName>
<tag>
<systemTagName>region_al</systemTagName>
<displayTagName>AL </displayTagName>
</tag>
</tagset>

 

 

 

oka standalone program lo dheeni string ga theesukoni

 

<tagsets><tagset> <systemTagSetName>topic</systemTagSetName><displayTagSetName>Topic</displayTagSetName><tag><systemTagName>9005482</systemTagName><displayTagName>Forgot username or password</displayTagName></tag></tagset><tagset> <systemTagSetName>region</systemTagSetName><displayTagSetName>Region</displayTagSetName><tag><systemTagName>region_al</systemTagName><displayTagName>AL </displayTagName></tag><tag><systemTagName>region_ky</systemTagName><displayTagName> KY </displayTagName></tag></tagset><tagset> <systemTagSetName>slctrgrp</systemTagSetName><displayTagSetName>Selector Grouping</displayTagSetName><tag><systemTagName>slctrgrp_ff15</systemTagName><displayTagName>Firefox 1+5</displayTagName></tag><tag><systemTagName>slctrgrp_ffall</systemTagName><displayTagName>Firefox (all)</displayTagName></tag></tagset><tagset> <systemTagSetName>srvgroup</systemTagSetName><displayTagSetName>Service</displayTagSetName><tag><systemTagName>srvgroup_dsl</systemTagName><displayTagName>DSL</displayTagName></tag></tagset><tagset> <systemTagSetName>device</systemTagSetName><displayTagSetName>Device</displayTagSetName><tag><systemTagName>dev_kindlefirehdx7</systemTagName><displayTagName>KindleFireHDX7</displayTagName></tag></tagset></tagsets>

 

 

dheeni read cheyyali generic ga

 

package com.sample.json;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class TagsetConversion {

    private static String tagfield = "<body><excludespanish>false</excludespanish><description><p>Testing Draft Article</p><p><br data-mce-bogus=1></p><p><strong>Testing Draft Article</strong></p><p><strong><br data-mce-bogus=1></strong></p><p>&quot;Testing Draft Article&quot;</p></description><publicSectionContent></publicSectionContent><relatedContent></relatedContent><title>Testing Draft Article</title><attachments></attachments><publishedDate>2015-08-09T17:15:16.205+00:00</publishedDate><expiredDate></expiredDate><lastModifiedBy>kmmanager</lastModifiedBy><owner>kmmanager</owner><weighting></weighting><status>PUBLISHED</status><tagsets><tagset> <systemTagSetName>topic</systemTagSetName><displayTagSetName>Topic</displayTagSetName><tag><systemTagName>9005482</systemTagName><displayTagName>Forgot username or password</displayTagName></tag></tagset><tagset> <systemTagSetName>region</systemTagSetName><displayTagSetName>Region</displayTagSetName><tag><systemTagName>region_al</systemTagName><displayTagName>AL </displayTagName></tag><tag><systemTagName>region_ky</systemTagName><displayTagName> KY </displayTagName></tag></tagset><tagset> <systemTagSetName>slctrgrp</systemTagSetName><displayTagSetName>Selector Grouping</displayTagSetName><tag><systemTagName>slctrgrp_ff15</systemTagName><displayTagName>Firefox 1+5</displayTagName></tag><tag><systemTagName>slctrgrp_ffall</systemTagName><displayTagName>Firefox (all)</displayTagName></tag></tagset><tagset> <systemTagSetName>srvgroup</systemTagSetName><displayTagSetName>Service</displayTagSetName><tag><systemTagName>srvgroup_dsl</systemTagName><displayTagName>DSL</displayTagName></tag></tagset><tagset> <systemTagSetName>device</systemTagSetName><displayTagSetName>Device</displayTagSetName><tag><systemTagName>dev_kindlefirehdx7</systemTagName><displayTagName>KindleFireHDX7</displayTagName></tag></tagset></tagsets></body>";

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Map<String, String> valueMap = new HashMap<String, String>();

        Map<String, Map> tagMap = new HashMap<String, Map>();

        Map<String, Map> tagSetMap = new HashMap<String, Map>();

        int position = tagfield.indexOf("<tagsets>");

        int closingposition = tagfield.indexOf("</tagsets>");

        String response = tagfield.substring(position + 9, closingposition);

        System.out.println("response :" + response);

        StringBuffer tagstrbfr = new StringBuffer(response);

        while (tagstrbfr.indexOf("<tagset") != -1) {
            StringBuffer strBfr = new StringBuffer(tagstrbfr.substring(8,
                    tagstrbfr.indexOf("</tagset>")));

            int tagStrBeginPosition = strBfr.indexOf("<");
            int tagStrEndPosition = strBfr.indexOf(">");

            String tagNodeKey = strBfr.substring(tagStrBeginPosition + 1,
                    tagStrEndPosition);

            valueMap = new HashMap<String, String>();

            while (strBfr.indexOf("<tag") != -1) {
                System.out.println(strBfr.indexOf("<"));

                int strBeginPosition = strBfr.indexOf("<");
                int strEndPosition = strBfr.indexOf(">");

                String nodeKey = strBfr.substring(strBeginPosition + 1,
                        strEndPosition);

                int nodeKeyLength = nodeKey.length();

                System.out.println("node Key :" + nodeKey);
                System.out.println("node key Length :" + nodeKeyLength);

                int nodeStartPosition = strBfr.indexOf("<" + nodeKey + ">");
                int nodeEndPosition = strBfr.indexOf("</" + nodeKey + ">");

                System.out.println("here u fil : "
                        + strBfr.substring(nodeStartPosition + nodeKeyLength
                                + 2, nodeEndPosition));

                String nodeValue = strBfr.substring(nodeStartPosition
                        + nodeKeyLength + 2, nodeEndPosition);

                int nodeValueLength = nodeValue.length();
                System.out.println("The nodeValue  " + nodeValue);
                System.out.println("node Value length  :" + nodeValueLength);

                int truncateLength = nodeValueLength + nodeKeyLength * 2 + 5;
                System.out.println(truncateLength);
                System.out.println(strBfr.substring(truncateLength));
                strBfr = new StringBuffer(strBfr.substring(truncateLength));
                System.out
                        .println("Breaking here for some reason : " + nodeKey);
                valueMap.put(nodeKey, nodeValue);

                for (Map.Entry<String, String> entry : valueMap.entrySet()) {
                    System.out.println(entry.getKey() + "/" + entry.getValue());
                }

            }
            tagMap.put(tagNodeKey, valueMap);

        }

        try {
            String mapAsJson = new ObjectMapper().writeValueAsString(tagMap);

            System.out.println("The Map Value is: " + mapAsJson);
        } catch (JsonGenerationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

kaani naaku loop avvadam ledhu.... eee logioc lo em cheangae cheyyali... inputs pls

Posted

use SAX for reading xml content...it will b very easy

Posted

xml kadhu mayya... different scenarion idhi..... eeee string mothanni tags ga divide chesi mAp lo key value pairs ga vadali

 

Posted

xml kadhu mayya... different scenarion idhi..... eeee string mothanni tags ga divide chesi mAp lo key value pairs ga vadali

widout looping?   :3D_Smiles:

Posted

widout looping?   :3D_Smiles:

 

loop avvali kani eee parders vadakoodahu..andhuku antey once i get key value pairs aaa map ni json ki marchali

Posted

Use SAX parser and just read element names ..... and element values ...

 

naku oka scenario undhi....  like below

 

<tagset>
<systemTagSetName>topic</systemTagSetName>
<displayTagSetName>Topic</displayTagSetName>
<tag>
<systemTagName>9005482</systemTagName>
<displayTagName>Forgot username or password</displayTagName>
</tag
</tagset>
<tagset>
<systemTagSetName>region</systemTagSetName>
<displayTagSetName>Region</displayTagSetName>
<tag>
<systemTagName>region_al</systemTagName>
<displayTagName>AL </displayTagName>
</tag>
</tagset>

 

 

 

oka standalone program lo dheeni string ga theesukoni

 

<tagsets><tagset> <systemTagSetName>topic</systemTagSetName><displayTagSetName>Topic</displayTagSetName><tag><systemTagName>9005482</systemTagName><displayTagName>Forgot username or password</displayTagName></tag></tagset><tagset> <systemTagSetName>region</systemTagSetName><displayTagSetName>Region</displayTagSetName><tag><systemTagName>region_al</systemTagName><displayTagName>AL </displayTagName></tag><tag><systemTagName>region_ky</systemTagName><displayTagName> KY </displayTagName></tag></tagset><tagset> <systemTagSetName>slctrgrp</systemTagSetName><displayTagSetName>Selector Grouping</displayTagSetName><tag><systemTagName>slctrgrp_ff15</systemTagName><displayTagName>Firefox 1+5</displayTagName></tag><tag><systemTagName>slctrgrp_ffall</systemTagName><displayTagName>Firefox (all)</displayTagName></tag></tagset><tagset> <systemTagSetName>srvgroup</systemTagSetName><displayTagSetName>Service</displayTagSetName><tag><systemTagName>srvgroup_dsl</systemTagName><displayTagName>DSL</displayTagName></tag></tagset><tagset> <systemTagSetName>device</systemTagSetName><displayTagSetName>Device</displayTagSetName><tag><systemTagName>dev_kindlefirehdx7</systemTagName><displayTagName>KindleFireHDX7</displayTagName></tag></tagset></tagsets>

 

 

dheeni read cheyyali generic ga

 

package com.sample.json;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class TagsetConversion {

    private static String tagfield = "<body><excludespanish>false</excludespanish><description><p>Testing Draft Article</p><p><br data-mce-bogus=1></p><p><strong>Testing Draft Article</strong></p><p><strong><br data-mce-bogus=1></strong></p><p>&quot;Testing Draft Article&quot;</p></description><publicSectionContent></publicSectionContent><relatedContent></relatedContent><title>Testing Draft Article</title><attachments></attachments><publishedDate>2015-08-09T17:15:16.205+00:00</publishedDate><expiredDate></expiredDate><lastModifiedBy>kmmanager</lastModifiedBy><owner>kmmanager</owner><weighting></weighting><status>PUBLISHED</status><tagsets><tagset> <systemTagSetName>topic</systemTagSetName><displayTagSetName>Topic</displayTagSetName><tag><systemTagName>9005482</systemTagName><displayTagName>Forgot username or password</displayTagName></tag></tagset><tagset> <systemTagSetName>region</systemTagSetName><displayTagSetName>Region</displayTagSetName><tag><systemTagName>region_al</systemTagName><displayTagName>AL </displayTagName></tag><tag><systemTagName>region_ky</systemTagName><displayTagName> KY </displayTagName></tag></tagset><tagset> <systemTagSetName>slctrgrp</systemTagSetName><displayTagSetName>Selector Grouping</displayTagSetName><tag><systemTagName>slctrgrp_ff15</systemTagName><displayTagName>Firefox 1+5</displayTagName></tag><tag><systemTagName>slctrgrp_ffall</systemTagName><displayTagName>Firefox (all)</displayTagName></tag></tagset><tagset> <systemTagSetName>srvgroup</systemTagSetName><displayTagSetName>Service</displayTagSetName><tag><systemTagName>srvgroup_dsl</systemTagName><displayTagName>DSL</displayTagName></tag></tagset><tagset> <systemTagSetName>device</systemTagSetName><displayTagSetName>Device</displayTagSetName><tag><systemTagName>dev_kindlefirehdx7</systemTagName><displayTagName>KindleFireHDX7</displayTagName></tag></tagset></tagsets></body>";

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Map<String, String> valueMap = new HashMap<String, String>();

        Map<String, Map> tagMap = new HashMap<String, Map>();

        Map<String, Map> tagSetMap = new HashMap<String, Map>();

        int position = tagfield.indexOf("<tagsets>");

        int closingposition = tagfield.indexOf("</tagsets>");

        String response = tagfield.substring(position + 9, closingposition);

        System.out.println("response :" + response);

        StringBuffer tagstrbfr = new StringBuffer(response);

        while (tagstrbfr.indexOf("<tagset") != -1) {
            StringBuffer strBfr = new StringBuffer(tagstrbfr.substring(8,
                    tagstrbfr.indexOf("</tagset>")));

            int tagStrBeginPosition = strBfr.indexOf("<");
            int tagStrEndPosition = strBfr.indexOf(">");

            String tagNodeKey = strBfr.substring(tagStrBeginPosition + 1,
                    tagStrEndPosition);

            valueMap = new HashMap<String, String>();

            while (strBfr.indexOf("<tag") != -1) {
                System.out.println(strBfr.indexOf("<"));

                int strBeginPosition = strBfr.indexOf("<");
                int strEndPosition = strBfr.indexOf(">");

                String nodeKey = strBfr.substring(strBeginPosition + 1,
                        strEndPosition);

                int nodeKeyLength = nodeKey.length();

                System.out.println("node Key :" + nodeKey);
                System.out.println("node key Length :" + nodeKeyLength);

                int nodeStartPosition = strBfr.indexOf("<" + nodeKey + ">");
                int nodeEndPosition = strBfr.indexOf("</" + nodeKey + ">");

                System.out.println("here u fil : "
                        + strBfr.substring(nodeStartPosition + nodeKeyLength
                                + 2, nodeEndPosition));

                String nodeValue = strBfr.substring(nodeStartPosition
                        + nodeKeyLength + 2, nodeEndPosition);

                int nodeValueLength = nodeValue.length();
                System.out.println("The nodeValue  " + nodeValue);
                System.out.println("node Value length  :" + nodeValueLength);

                int truncateLength = nodeValueLength + nodeKeyLength * 2 + 5;
                System.out.println(truncateLength);
                System.out.println(strBfr.substring(truncateLength));
                strBfr = new StringBuffer(strBfr.substring(truncateLength));
                System.out
                        .println("Breaking here for some reason : " + nodeKey);
                valueMap.put(nodeKey, nodeValue);

                for (Map.Entry<String, String> entry : valueMap.entrySet()) {
                    System.out.println(entry.getKey() + "/" + entry.getValue());
                }

            }
            tagMap.put(tagNodeKey, valueMap);

        }

        try {
            String mapAsJson = new ObjectMapper().writeValueAsString(tagMap);

            System.out.println("The Map Value is: " + mapAsJson);
        } catch (JsonGenerationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

kaani naaku loop avvadam ledhu.... eee logioc lo em cheangae cheyyali... inputs pls

 

Posted

loop avvali kani eee parders vadakoodahu..andhuku antey once i get key value pairs aaa map ni json ki marchali

 

Sorry didn't read this ..

How ever you have to use parser to read XMLstring as xml

and then use xml to json converters
 

Posted

something i have tried, if it helps fine otherwise plz ignore

 

 
 
/**
* @param args
*/
static String[] elements = { "tagsets", "tagset", "tag", "displayTagName" };;
static String input = "";
 
public static void main(String[] args) {
// TODO Auto-generated method stub
input = "<body><excludespanish>false</excludespanish><description>"
+ "<p>Testing Draft Article</p><p><br data-mce-bogus=1></p><p><strong>Testing Draft Article</strong></p>"
+ "<p><strong><br data-mce-bogus=1></strong></p><p>&quot;Testing Draft Article&quot;"
+ "</p></description><publicSectionContent></publicSectionContent><relatedContent>"
+ "</relatedContent><title>Testing Draft Article</title><attachments></attachments>"
+ "<publishedDate>2015-08-09T17:15:16.205+00:00</publishedDate><expiredDate></expiredDate>"
+ "<lastModifiedBy>kmmanager</lastModifiedBy><owner>kmmanager</owner><weighting></weighting>"
+ "<status>PUBLISHED</status><tagsets><tagset> <systemTagSetName>topic</systemTagSetName>"
+ "<displayTagSetName>Topic</displayTagSetName><tag><systemTagName>9005482</systemTagName>"
+ "<displayTagName>Forgot username or password</displayTagName></tag></tagset><tagset> "
+ "<systemTagSetName>region</systemTagSetName><displayTagSetName>Region</displayTagSetName>"
+ "<tag><systemTagName>region_al</systemTagName><displayTagName>AL </displayTagName></tag>"
+ "<tag><systemTagName>region_ky</systemTagName><displayTagName> KY </displayTagName></tag>"
+ "</tagset><tagset> <systemTagSetName>slctrgrp</systemTagSetName><displayTagSetName>Selector Grouping"
+ "</displayTagSetName><tag><systemTagName>slctrgrp_ff15</systemTagName><displayTagName>Firefox 1+5"
+ "</displayTagName></tag><tag><systemTagName>slctrgrp_ffall</systemTagName><displayTagName>Firefox (all)</displayTagName></tag></tagset><tagset> <systemTagSetName>srvgroup</systemTagSetName><displayTagSetName>Service</displayTagSetName><tag><systemTagName>srvgroup_dsl</systemTagName><displayTagName>DSL</displayTagName></tag></tagset><tagset> <systemTagSetName>device</systemTagSetName><displayTagSetName>Device</displayTagSetName><tag><systemTagName>dev_kindlefirehdx7</systemTagName><displayTagName>KindleFireHDX7</displayTagName></tag></tagset></tagsets></body>";
 
String[] inputs = { "", "", "" };
findValue(input);
}
 
private static void findValue(String input) {
// TODO Auto-generated method stub
for (int i = 0; i < elements.length; i++) {
Pattern MY_PATTERN = Pattern.compile("<" + elements[i] + ">(.*?)</"
+ elements[i] + ">");
Matcher m = MY_PATTERN.matcher(input);
while (m.find()) {
System.out.println("===============" + elements[i]);
input = m.group(1);
System.out.println(input);
if (i + 1 < elements.length && input.contains(elements[i + 1])) {
findValue(input);
}
 
}
}
}
 
Posted

sorry didn't read this ..

How ever you have to use parser to read XMLstring as xml

and then use xml to json converters
 

 

i have to readit as  s string and then loop over each tabs in it and considering the tag name as kety and value as value..... and keeping both of them in a MAP.
 

Posted

i have to readit as  s string and then loop over each tabs in it and considering the tag name as kety and value as value..... and keeping both of them in a MAP.
 

 

I understand that .... Just curious (no need to answer .. if you dont want)

1. Whats stopping you to use parser and then read it as doc.getElementName as Key and value as map value ?
 

Posted

I understand that .... Just curious (no need to answer .. if you dont want)

1. Whats stopping you to use parser and then read it as doc.getElementName as Key and value as map value ?
 

 

I have a response body in my rEST call ..... we want to modify the body tag to json....... as we are using produces by default as json due to th ehtml elements we are getting some additional tags..... so we are forced to break the body as string and do some massaging on it

Posted

response = new String Buffer (response)
while (position of tagset in 'response str' != -1) {
String tempStr = new String (string between start and end positions of tagset)
    read systemTagSetName
    read displayTagSetName
    while (position of tag in 'tempStr' != -1) {
        tempstr2 = new String (string between start and end positions of tag)
        tempStr = new String(end position of last tag , end of tempStr)
    }
response str = new response str(end position of last tagset , end of actual string)
}

Posted

I have a response body in my rEST call ..... we want to modify the body tag to json....... as we are using produces by default as json due to th ehtml elements we are getting some additional tags..... so we are forced to break the body as string and do some massaging on it

 

Ideally you shouldn't be doing string manipulations on xml string like this ... We should always use parsers.

If you have code reviews or code walk through .. u might get questions.

 

Sorry if anything I told, didn't make any sense

×
×
  • Create New...