문제

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.

도움이 되었습니까?

해결책

        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));
        }

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top