List of usage examples for org.w3c.dom Element getTextContent
public String getTextContent() throws DOMException;
From source file:Main.java
public static void main(String arg[]) throws Exception { String xmlRecords = "<root><x>1</x><x>2</x><x>3</x><x>4</x></root>"; DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xmlRecords)); Document doc = db.parse(is);/*from w w w . jav a2 s . com*/ NodeList nodes = doc.getElementsByTagName("x"); System.out.println(nodes.getLength()); List<String> valueList = new ArrayList<String>(); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); String name = element.getTextContent(); // Element line = (Element) name.item(0); System.out.println("Name: " + name); valueList.add(name); } }
From source file:Main.java
public static void main(String[] args) throws Exception { String xmlfile = "data.xml"; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(xmlfile); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); Element element = (Element) xpath.evaluate("/root/param[@name='name2']", doc, XPathConstants.NODE); System.out.println(element.getTextContent()); }
From source file:Main.java
public static void main(String[] args) throws Exception { InputSource source = new InputSource(new StringReader("<root>\n" + "<field name='firstname'>\n" + " <value>John</value>\n" + "</field>\n" + "<field name='lastname'>\n" + " <value>Citizen</value>\n" + "</field>\n" + "<field name='DoB'>\n" + " <value>01/01/1980</value>\n" + "</field>\n" + "<field name='Profession'>\n" + " <value>Manager</value>\n" + "</field>\n" + "</root>")); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); Document document = documentBuilder.parse(source); NodeList allFields = (NodeList) document.getElementsByTagName("field"); Map<String, String> data = new HashMap<>(); for (int i = 0; i < allFields.getLength(); i++) { Element field = (Element) allFields.item(i); String nameAttribute = field.getAttribute("name"); Element child = (Element) field.getElementsByTagName("value").item(0); String value = child.getTextContent(); data.put(nameAttribute, value);//w ww .j a va2 s . c o m } for (Map.Entry field : data.entrySet()) { System.out.println(field.getKey() + ": " + field.getValue()); } }
From source file:OldExtractor.java
/** * @param args the command line arguments *//*from w ww.j a v a 2 s .c o m*/ public static void main(String[] args) { // TODO code application logic here String bingUrl = "https://api.datamarket.azure.com/Bing/Search/Web?$top=10&$format=Atom&Query=%27gates%27"; //Provide your account key here. String accountKey = "ghTYY7wD6LpyxUO9VRR7e1f98WFhHWYERMcw87aQTqQ"; // String accountKey = "xqbCjT87/MQz25JWdRzgMHdPkGYnOz77IYmP5FUIgC8"; byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes()); String accountKeyEnc = new String(accountKeyBytes); try { URL url = new URL(bingUrl); URLConnection urlConnection = url.openConnection(); urlConnection.setRequestProperty("Authorization", "Basic " + accountKeyEnc); InputStream inputStream = (InputStream) urlConnection.getContent(); byte[] contentRaw = new byte[urlConnection.getContentLength()]; inputStream.read(contentRaw); String content = new String(contentRaw); //System.out.println(content); try { File file = new File("Results.xml"); FileWriter fileWriter = new FileWriter(file); fileWriter.write(content); //fileWriter.write("a test"); fileWriter.flush(); fileWriter.close(); } catch (IOException e) { System.out.println(e); } DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { //System.out.println("here"); //Using factory get an instance of document builder DocumentBuilder db = dbf.newDocumentBuilder(); //parse using builder to get DOM representation of the XML file Document dom = db.parse("Results.xml"); Element docEle = (Element) dom.getDocumentElement(); //get a nodelist of elements NodeList nl = docEle.getElementsByTagName("d:Url"); if (nl != null && nl.getLength() > 0) { for (int i = 0; i < nl.getLength(); i++) { //get the employee element Element el = (Element) nl.item(i); // System.out.println("here"); System.out.println(el.getTextContent()); //get the Employee object //Employee e = getEmployee(el); //add it to list //myEmpls.add(e); } } NodeList n2 = docEle.getElementsByTagName("d:Title"); if (n2 != null && n2.getLength() > 0) { for (int i = 0; i < n2.getLength(); i++) { //get the employee element Element e2 = (Element) n2.item(i); // System.out.println("here"); System.out.println(e2.getTextContent()); //get the Employee object //Employee e = getEmployee(el); //add it to list //myEmpls.add(e); } } NodeList n3 = docEle.getElementsByTagName("d:Description"); if (n3 != null && n3.getLength() > 0) { for (int i = 0; i < n3.getLength(); i++) { //get the employee element Element e3 = (Element) n3.item(i); // System.out.println("here"); System.out.println(e3.getTextContent()); //get the Employee object //Employee e = getEmployee(el); //add it to list //myEmpls.add(e); } } } catch (SAXException se) { se.printStackTrace(); } catch (ParserConfigurationException pe) { pe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } catch (IOException e) { System.out.println(e); } //The content string is the xml/json output from Bing. }
From source file:Main.java
public static String getSelf(Element e) { return e.getTextContent(); }
From source file:Main.java
public static String getElementText(Element elem) { return elem.getTextContent(); }
From source file:Main.java
static String getElementText(Element element) { return element.getTextContent(); }
From source file:Main.java
public static String getContentText(Element element) { return element.getTextContent(); }
From source file:Main.java
public static String getChildAsString(Element e, String child) { NodeList nodes = e.getElementsByTagName(child); if (nodes.getLength() > 0) { Element i = (Element) nodes.item(0); return i.getTextContent().trim(); } else/*from w w w. j ava 2 s . com*/ return null; }
From source file:Main.java
static public String getTrimmedTextContent(Element element) { return (element != null) ? element.getTextContent().trim() : null; }