Domanda

Hi guys i have been given a task to parse a string which will be coming from the server.

The string looks like:

<first name=$Jon$ last name=$Doe$/><first name=$Doe$ last name=$Jon$/><first name=$r$ last name=$k$/>

and the output needed is:

first name: Jon
last name: Doe
-------------------
first name: Doe
last name: Jon
-------------------
first name: r
last name: k
-------------------

i.e.,

key: value

I have done some simple text-parsing which included a simple delimiter like a $ or a %. but in this case i don't understand how to parse the text. Your help will be very helpful.

È stato utile?

Soluzione

        Matcher keys = Pattern.compile("[<\\s)](.*?)[=]").matcher(string);
        Matcher values = Pattern.compile("[$](.*?)[$]").matcher(string);
        while(keys.find() && values.find()) {
            System.out.println(keys.group(1)+" : "+values.group(1));
        }

Altri suggerimenti

replace $ in xml string from server with ", load it as xml document, use XPath or some other mechanism to parse the information you need

There can be multiple ways to reach to the solution

  1. Can use XSLT with Java. (Java provides apis like TransformerFactory, Transformer etc.)
  2. Can use XSLT in IDE like eclipse. Several plugins available.

Can check this out www.vogella.com/articles/XSLT/article.html

  1. can use unix script to do the same.

    How to convert xml file in to a property file using unix shell script

It is not the exact solution to your problem but solutions you can try. Similarly there can be many other ways for sure.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top