-2

I have an XML document like this:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <user>
        <name>John</name>
    </user>
    <company>
        <name>MyCompany></name>
    </company>
</root>

I need to extract the value "MyCompany" (as a String value) from node, and I don't know how to locate it. I don't need the other elements from the document. I extracted from the xml file the org.w3c.dom.Document instance.

Catalin Vladu
  • 389
  • 1
  • 6
  • 17

1 Answers1

0

Try it with jsoup

Example:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;

public class NewClass {

    public static void main(String args[]) {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                        "<user>\n" +
                        "     <name>John</name>\n" +
                        "</user>\n" +
                        "<company>\n" +
                        "     <name>MyCompany</name>\n" +
                        "</company>";
        Document doc = Jsoup.parse(xml,"",Parser.xmlParser());
        for (Element e : doc.select("company")) {
            System.out.println(e.text());
        }
   }
} 
Eritrean
  • 15,851
  • 3
  • 22
  • 28