Java XML CDATA Get getCdata(Element elem)

Here you can find the source of getCdata(Element elem)

Description

Extracts the first CDATA child from the given org.w3c.dom.Element .

License

Apache License

Parameter

Parameter Description
elem the parent Element

Return

the String value of the CDATA section, or null if none found

Declaration

public static String getCdata(Element elem) 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import org.w3c.dom.CharacterData;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**// w w w  .j  av  a  2s . c om
     * Extracts the first CDATA child from the given {@code org.w3c.dom.Element}.
     *
     * @param elem the parent Element
     * @return the String value of the CDATA section, or {@code null} if none
     *         found
     */
    public static String getCdata(Element elem) {
        NodeList nodes = elem.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
                CharacterData cdataNode = (CharacterData) node;
                return cdataNode.getData();
            }
        }
        return null;
    }
}

Related

  1. getCDATA(Element elem)
  2. getCData(Element element)
  3. getCDataNode(Element element)
  4. getCDataValue(Node node, String tag)
  5. getChildCdata(Node element, String name)