The_Monster
The_Monster

Reputation: 510

how to exclude tag from XML String in java

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

Upvotes: 1

Views: 2386

Answers (5)

jedison
jedison

Reputation: 976

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.

Upvotes: 2

Mifmif
Mifmif

Reputation: 3190

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

Upvotes: 0

swapnil gandhi
swapnil gandhi

Reputation: 816

use DOMParser in java. Check further in java docs

Upvotes: 2

MadProgrammer
MadProgrammer

Reputation: 347194

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

Upvotes: 1

CocoNess
CocoNess

Reputation: 4222

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

Oracle Java XML Parser Tutorial

Upvotes: 1

Related Questions