DocumentHelper xml工具包

admin 2025-08-17 06:46:25 2018世界杯投注

DocumentHelper xml工具包

DocumentHelper xml工具包

最新推荐文章于 2022-08-02 16:03:26 发布

Coolyqq

最新推荐文章于 2022-08-02 16:03:26 发布

阅读量5.2k

收藏

点赞数

CC 4.0 BY-SA版权

分类专栏:

java

文章标签:

xml

xml工具

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/Coolyqq/article/details/40116355

java

专栏收录该内容

42 篇文章

订阅专栏

本文介绍了一个用于解析XML文档的Java工具类,提供了多种方法来帮助开发者读取和操作XML文件,包括从输入流和文件中解析文档、获取指定名称的子元素、提取属性值等。

摘要生成于

C知道

,由 DeepSeek-R1 满血版支持,

前往体验 >

public class DocumentHelper

{

public static Document parse(InputStream is)

throws ParserConfigurationException, IOException, SAXException

{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setNamespaceAware(true);

Document document = factory.newDocumentBuilder().parse(is);

return document;

}

public static Document parse(File file) throws ParserConfigurationException, IOException, SAXException {

InputStream is = null;

try {

is = new FileInputStream(file);

Document localDocument = parse(is);

return localDocument;

}

finally

{

if (is != null)

is.close();

}

}

public static Element getElementChild(Element element, String name, boolean required) throws Exception {

NodeList nodes = element.getChildNodes();

for (int i = 0; i < nodes.getLength(); ++i) {

Node node = nodes.item(i);

if ((node instanceof Element) && (node.getLocalName().equals(name)) && (node.getNamespaceURI() == null)) {

return ((Element)node);

}

}

if (required) {

throw new Exception("Missing element " + name + " in " + element.getLocalName());

}

return null;

}

public static Element[] getElementChildren(Element element) {

List elements = new ArrayList();

NodeList nodes = element.getChildNodes();

for (int i = 0; i < nodes.getLength(); ++i) {

if (nodes.item(i) instanceof Element) {

elements.add((Element)nodes.item(i));

}

}

return ((Element[])elements.toArray(new Element[0]));

}

public static Element[] getElementChildren(Element element, String name) {

Element[] elements = getElementChildren(element);

List taggedElements = new ArrayList();

for (int i = 0; i < elements.length; ++i) {

if ((elements[i].getLocalName().equals(name)) && (elements[i].getNamespaceURI() == null)) {

taggedElements.add(elements[i]);

}

}

return ((Element[])taggedElements.toArray(new Element[0]));

}

public static String getAttribute(Element element, String name, boolean required) throws Exception {

if (!(element.hasAttribute(name))) {

if (required) {

throw new Exception("Missing attribute " + name + " on element " + element.getLocalName());

}

return null;

}

return element.getAttribute(name);

}

public static boolean getBooleanAttribute(Element element, String name, boolean defaultValue) throws Exception {

String value = element.getAttribute(name);

if (value.equals("")) {

return defaultValue;

}

return value.equalsIgnoreCase("true");

}

public static boolean getBooleanElement(Element element, String name, boolean defaultValue) throws Exception {

Element child = getElementChild(element, name, false);

if (child == null) {

return defaultValue;

}

return getElementText(child, true).equalsIgnoreCase("true");

}

public static String getStringElement(Element element, String name, String defaultValue) throws Exception {

Element child = getElementChild(element, name, false);

if (child == null) {

return defaultValue;

}

return getElementText(child, true);

}

public static int getIntegerElement(Element element, String name, int defaultValue) throws Exception {

Element child = getElementChild(element, name, false);

if (child == null) {

return defaultValue;

}

String text = getElementText(child, true);

try {

return Integer.parseInt(text);

} catch (NumberFormatException e) {

throw new RuntimeException("Invalid integer value " + text + " in element " + name);

}

}

public static String getElementText(Element element, boolean required) throws Exception {

StringBuilder text = new StringBuilder(30);

NodeList nodes = element.getChildNodes();

for (int i = 0; i < nodes.getLength(); ++i) {

Node node = nodes.item(i);

if (node instanceof Text) {

text.append(node.getNodeValue());

}

}

if ((required) && (text.length() == 0))

throw new Exception("Missing text content in element " + element.getLocalName());

if (text.length() == 0) {

return null;

}

return text.toString();

}

}