Java XML Node Text Value getPropText(Node node)

Here you can find the source of getPropText(Node node)

Description

get Prop Text

License

Open Source License

Declaration

public static Properties getPropText(Node node) 

Method Source Code

//package com.java2s;
/**//from w  w  w  .  j  av  a2  s .  c  o  m
 * Copyright (c) 2001-2002. Department of Family Medicine, McMaster University. All Rights Reserved.
 * This software is published under the GPL GNU General Public License.
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * This software was written for the
 * Department of Family Medicine
 * McMaster University
 * Hamilton
 * Ontario, Canada
 */

import java.util.Properties;

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

public class Main {
    static Properties prop = null;

    public static Properties getPropText(Node node) {
        prop = new Properties();
        setPropText(node);
        return prop;
    }

    public static Properties getPropText(Node node, String tagName, String attrName1, String attrName2) {
        prop = new Properties();
        setPropText(node, tagName, attrName1, attrName2);
        return prop;
    }

    public static Properties getPropText(Node node, String tagName, String attrName1) {
        prop = new Properties();
        setPropText(node, tagName, attrName1);
        return prop;
    }

    public static void setPropText(Node node) {
        NodeList list = node.getChildNodes();

        if (list != null) {
            for (int i = 0; i < list.getLength(); i++) {
                setPropText(list.item(i));
            }
        }

        if (node.getNodeType() == Node.TEXT_NODE) {
            prop.setProperty(node.getParentNode().getNodeName(), node.getNodeValue());
        }
    }

    public static void setPropText(Node node, String tag, String attr1, String attr2) {
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();

        NodeList list = node.getChildNodes();

        if (list != null) {
            for (int i = 0; i < list.getLength(); i++) {
                setPropText(list.item(i), tag, attr1, attr2);
            }
        }

        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(tag)) {
                NamedNodeMap attrib = node.getAttributes();
                for (int i = 0; i < attrib.getLength(); i++) {
                    Node curAttr = attrib.item(i);
                    if (curAttr.getNodeName().equals(attr1))
                        sb1 = new StringBuilder(curAttr.getNodeValue());
                    if (curAttr.getNodeName().equals(attr2))
                        sb2 = new StringBuilder(curAttr.getNodeValue());
                }
                prop.setProperty(sb1.toString(), sb2.toString());
            }
        }
    }

    public static void setPropText(Node node, String tag, String attr1) {
        String attrName = "";
        NodeList list = node.getChildNodes();

        if (list != null) {
            for (int i = 0; i < list.getLength(); i++) {
                setPropText(list.item(i), tag, attr1);
            }
        }

        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(tag)) {
                NamedNodeMap attrib = node.getAttributes();
                for (int i = 0; i < attrib.getLength(); i++) {
                    Node curAttr = attrib.item(i);
                    if (curAttr.getNodeName().equals(attr1)) {
                        attrName = curAttr.getNodeValue();
                        NodeList oldlist = node.getChildNodes();
                        for (int j = 0; j < oldlist.getLength(); j++) {

                            if (oldlist.item(j).getNodeType() == Node.TEXT_NODE) {
                                prop.setProperty(attrName, oldlist.item(j).getNodeValue());
                            }

                        }

                    }
                }
            }
        }
    }
}

Related

  1. getNodeTextValue(Node pElement)
  2. getNormalizedText(Node node)
  3. getPlainTextBelow(Node n)
  4. getPropertiesFromXML(Node propNode)
  5. getPropertiesFromXML(Node propNode)
  6. getRawContent(Node n)
  7. getSimpleElementText(final Node node)
  8. getSimpleNodeValue(Node node)
  9. getSingleNodeTextContent(Node node, String path)