get XML Child Element As Point - Java XML

Java examples for XML:XML Element Child

Description

get XML Child Element As Point

Demo Code


//package com.java2s;
import java.awt.Point;
import org.w3c.dom.Element;

public class Main {
    public static Point getChildAsPoint(Element parent, String childName) {
        if (doesElementContainChildren(parent, childName)) {
            String pointString = getFirstChildElement(parent, childName)
                    .getTextContent();/*from  www . java  2s .  c om*/
            String[] coords = pointString.split(",");
            if (coords.length == 2) {
                Point point = new Point(Integer.parseInt(coords[0]
                        .replaceAll(" ", "")), Integer.parseInt(coords[1]
                        .replaceAll(" ", "")));
                return point;
            }
        }
        return null;
    }

    public static boolean doesElementContainChildren(Element parent,
            String... childNames) {
        boolean missingChild = false;
        for (String childName : childNames) {
            if (parent.getElementsByTagName(childName).getLength() == 0) {
                missingChild = true;
            }
        }
        if (missingChild)
            return false;
        else
            return true;
    }

    public static Element getFirstChildElement(Element parent,
            String childName) {
        if (parent.getElementsByTagName(childName).getLength() > 0) {
            return (Element) parent.getElementsByTagName(childName).item(0);
        }
        return null;
    }
}

Related Tutorials