Пример #1
0
CL_String CL_DomNode::select_string(const CL_DomString &xpath_expression) const
{
	CL_DomNode node = select_node(xpath_expression);
	if (node.is_element())
		return node.to_element().get_text();
	else
		return node.get_node_value();
}
Пример #2
0
CL_String CL_DomElement::get_text() const
{
	CL_String str;
	if (has_child_nodes() == false)
		return str;

	CL_DomNode cur = get_first_child();
	while (!cur.is_null())
	{
		if (cur.is_text() || cur.is_cdata_section())
			str.append(cur.get_node_value());
		if (cur.is_element())
			str.append(cur.to_element().get_text());
		cur = cur.get_next_sibling();
	}
	return str;
}
Пример #3
0
CL_DomString CL_DomNode::find_prefix(const CL_DomString &namespace_uri) const
{
	CL_DomElement cur = to_element();
	while (!cur.is_null())
	{
		CL_DomNamedNodeMap attributes = cur.get_attributes();
		int size = attributes.get_length();
		for (int index = 0; index < size; index++)
		{
			CL_DomNode attribute = attributes.item(index);
			if (attribute.get_prefix() == "xmlns" &&
				attribute.get_node_value() == namespace_uri)
			{
				return attribute.get_local_name();
			}
		}
		cur = cur.get_parent_node().to_element();
	}
	return CL_DomString();
}
Пример #4
0
//рекурсивное клонирование XML-элемента
CL_DomElement cloneElement(CL_DomElement &element, CL_DomDocument &doc)
{
	//создаем элемент-клон
	CL_DomElement clone(doc, element.get_tag_name());

	//копируем атрибуты
	CL_DomNamedNodeMap attributes = element.get_attributes();
	unsigned long length = attributes.get_length();
	for (unsigned long i = 0; i < length; ++i)
	{
		CL_DomNode attr = attributes.item(i);
		clone.set_attribute(attr.get_node_name(), attr.get_node_value());
	}

	//рекурсивно копируем дочерние элементы
	for (CL_DomElement child = element.get_first_child_element(); !child.is_null(); child = child.get_next_sibling_element())
		clone.append_child(cloneElement(child, doc));

	//возвращаем клонированный элемент
	return clone;
}
Пример #5
0
CL_DomNode CL_DomDocument::import_node(const CL_DomNode &node, bool deep)
{
	CL_DomNode imported_node;
	switch (node.get_node_type())
	{
	case NULL_NODE:
		return imported_node;
	case ELEMENT_NODE:
		imported_node = create_element_ns(node.get_namespace_uri(), node.get_node_name());
		break;
	case ATTRIBUTE_NODE:
		imported_node = create_attribute_ns(node.get_namespace_uri(), node.get_node_name());
		imported_node.set_node_value(node.get_node_value());
		break;
	case TEXT_NODE:
		imported_node = create_text_node(node.get_node_value());
		break;
	case CDATA_SECTION_NODE:
		imported_node = create_cdata_section(node.get_node_value());
		break;
	case ENTITY_REFERENCE_NODE:
		imported_node = create_entity_reference(node.get_node_name());
		break;
	case ENTITY_NODE:
		return imported_node;
	case PROCESSING_INSTRUCTION_NODE:
		imported_node = create_processing_instruction(node.to_processing_instruction().get_target(), node.to_processing_instruction().get_data());
		break;
	case COMMENT_NODE:
		imported_node = create_comment(node.get_node_value());
		break;
	case DOCUMENT_NODE:
		imported_node = create_document_fragment();
		break;
	case DOCUMENT_TYPE_NODE:
		return imported_node;
	case DOCUMENT_FRAGMENT_NODE:
		imported_node = create_document_fragment();
		break;
	case NOTATION_NODE:
		return imported_node;
	}

	if (node.is_element())
	{
		CL_DomElement import_element = imported_node.to_element();
		CL_DomNamedNodeMap node_attributes = node.get_attributes();
		int size = node_attributes.get_length();
		for (int index = 0; index < size; index++)
		{
			CL_DomNode attr = node_attributes.item(index);
			import_element.set_attribute_node_ns(import_node(attr, deep).to_attr());
		}
	}

	if (deep)
	{
		CL_DomNode cur = node.get_first_child();
		while (!cur.is_null())
		{
			imported_node.append_child(import_node(cur, true));
			cur = cur.get_next_sibling();
		}
	}

	return imported_node;
}