1

I am making a piece of code to send and recieve data from and to an webpage. I am doeing this in java. But when i 'receive' the xml data it is still between tags like this

<?xml version='1.0'?>
    <document>
        <title> TEST </title>
    </document>

How can i get the data without the tags in Java.

This is what i tried, The function writes the data and then should get the reponse and use that in a System.out.println.

public static String User_Select(String username, String password) {

        String mysql_type = "1"; // 1 = Select

        try {
            String urlParameters = "mysql_type=" + mysql_type + "&username=" + username + "&password=" + password;
            URL url = new URL("http://localhost:8080/HTTP_Connection/index.php");
            URLConnection conn = url.openConnection();

            conn.setDoOutput(true);

            OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

            writer.write(urlParameters);
            writer.flush();

            String line;
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                //System.out.println("Het werkt!!");
            }
            writer.close();
            reader.close();
            return line;

        } catch (IOException iox) {
            iox.printStackTrace();
            return null;
        }

    }

Thanks in advance

A. Agarwal
  • 415
  • 4
  • 12
The_Monster
  • 494
  • 2
  • 7
  • 28
  • 1
    If You whant to do a get request you shold add the `urlParameters to the URL and remove the part with setDoOutput and the writer. – Jens Jun 05 '14 at 09:23
  • Is what you are saying is that you want only "TEST" not the XML tags? – jedison Jun 05 '14 at 09:38
  • Yes @jedison thats what i am trying to say – The_Monster Jun 05 '14 at 09:41
  • thanks, posted a possible answer which should be much easier than JSoup, DOMParser, XML Parser, etc. – jedison Jun 06 '14 at 11:24
  • @The_Monster It's discouraged Regex to parse XML or HTML , check this http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Mifmif Jun 11 '14 at 09:12

5 Answers5

2

use DOMParser in java. Check further in java docs

swapnil gandhi
  • 816
  • 1
  • 20
  • 38
2

I would suggest simply using RegEx to read the XML, and get the tag content that you are after. That simplifies what you need to do, and limits the inclusion of additional (unnecessary) libraries. And then there are lots of StackOverflows on this topic: Regex for xml parsing and In RegEx, I want to find everything between two XML tags just to mention 2 of them.

Community
  • 1
  • 1
jedison
  • 908
  • 6
  • 15
  • It's discouraged Regex to parse XML or HTML , check this http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Mifmif Jun 11 '14 at 09:12
  • That is about parsing HTML and XHTML, not about XML. – jedison Jun 11 '14 at 11:01
1

Use an XML Parser to Parse your XML. Here is a link to Oracle's Tutorial

Oracle Java XML Parser Tutorial

CocoNess
  • 4,213
  • 4
  • 26
  • 43
1

Simply pass the InputStream from URLConnection

Document doc = DocumentBuilderFactory.
    newInstance(). 
    newDocumentBuilder().
    parse(conn.getInputStream());

From there you could use xPath to query the contents of the document or simply walk the document model.

Take a look at Java API for XML Processing (JAXP) for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You have to use an XML Parser , in your case the perfect choice is JSoup which scrap data from the web and parse XML & HTML format ,it will load data and parse it and give you what you want , here is a an example of how it works :

1. XML From an URL

String xml = Jsoup.connect("http://localhost:8080/HTTP_Connection/index.php")
            .get().toString();
Document doc = Jsoup.parse(xml, "", Parser.xmlParser());
String myTitle=doc.select("title").first();// myTitle contain now  TEST

Edit : to send GET or POST parameters with you request use this code:

 String xml = Jsoup.connect("http://localhost:8080/HTTP_Connection/index.php")
                .data("param1Name";"param1Value")
                .data("param2Name","param2Value").get().toString();

you can use get() to invoke HTTP GET method or post() to invoke HTTP POST method.

2. XML From String

You can use JSoup to parse XML data in a String :

String xmlData="<?xml version='1.0'?><document> <title> TEST </title> </document>" ;
Document doc = Jsoup.parse(xmlData, "", Parser.xmlParser());
String myTitle=doc.select("title").first();// myTitle contain now  TEST
Mifmif
  • 3,132
  • 18
  • 23