⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 domnodes.cpp

📁 简单的xml解析类
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		if( parent != 0 ) {
			myPath = parent->getPath() + "/" + myPath;

			const Array<DomElementNode*> siblings = getSiblingElementNodes();
			int count = 0, id = 0;
			for( int i = 0; i < siblings.getSize(); ++i ) {
				if( siblings[i]->getName() == m_name ) {
					++count;
					if( siblings[i] == this ) {
						id = count;
						break;
					}
				}
			}
			myPath += String::format("[%d]", id);
		}

		return (myPath);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	Array<DomElementNode*> DomElementNode::getElementNodes()
	{
		Array<DomElementNode*> nodes;
		for( int i = 0; i < m_nodes.getSize(); ++i ) {
			if( m_nodes[i]->getNodeType() == DomNode::ELEMENT ) {
				nodes.append( (DomElementNode*)m_nodes[i] );
			}
		}

		return (nodes);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	const Array<DomElementNode*> DomElementNode::getElementNodes() const
	{
		return ((DomElementNode*)this)->getElementNodes();
	}

	// ---------------------------------------------------------------------------------------------------------------------

	Array<DomElementNode*> DomElementNode::getSiblingElementNodes()
	{
		DomNode* parent = getParent();
		if( parent != 0 && parent->getNodeType() == DomNode::ELEMENT ) {
			return ((DomElementNode*)parent)->getElementNodes();
		}

		Array<DomElementNode*> results(1);
		results[0] = this;
		return (results);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	const Array<DomElementNode*> DomElementNode::getSiblingElementNodes() const
	{
		return ((DomElementNode*)this)->getSiblingElementNodes();
	}

	// ---------------------------------------------------------------------------------------------------------------------

	Array<DomTextNode*> DomElementNode::getTextNodes()
	{
		Array<DomTextNode*> nodes;
		for( int i = 0; i < m_nodes.getSize(); ++i ) {
			if( m_nodes[i]->getNodeType() == DomNode::TEXT || m_nodes[i]->getNodeType() == DomNode::CDATA ) {
				nodes.append( (DomTextNode*)m_nodes[i] );
			}
		}

		return (nodes);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	const Array<DomTextNode*> DomElementNode::getTextNodes() const
	{
		return ((DomElementNode*)this)->getTextNodes();
	}

	// ---------------------------------------------------------------------------------------------------------------------

	Array<DomAttributeNode*> DomElementNode::getAttributeNodes()
	{
		Array<DomAttributeNode*> nodes;
		for( int i = 0; i < m_nodes.getSize(); ++i ) {
			if( m_nodes[i]->getNodeType() == DomNode::ATTRIBUTE ) {
				nodes.append( (DomAttributeNode*)m_nodes[i] );
			}
		}

		return (nodes);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	const Array<DomAttributeNode*> DomElementNode::getAttributeNodes() const
	{
		return ((DomElementNode*)this)->getAttributeNodes();
	}

	// ---------------------------------------------------------------------------------------------------------------------

	bool DomElementNode::hasAttribute( String attribute ) const
	{
		return (getAttributeNode(attribute) != 0);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	DomAttributeNode* DomElementNode::getAttributeNode( String attribute )
	{
		Array<DomAttributeNode*> attributes = getAttributeNodes();
		for( int i = 0; i < attributes.getSize(); ++i ) {
			if( attributes[i]->getName() == attribute ) {
				return (attributes[i]);
			}
		}

		return (0);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	const DomAttributeNode* DomElementNode::getAttributeNode( String attribute ) const
	{
		return ((DomElementNode*)this)->getAttributeNode(attribute);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	Array<String> DomElementNode::getAttributeNames() const
	{
		Array<DomAttributeNode*> attributes = getAttributeNodes();
		Array<String> names(attributes.getSize());

		for( int i = 0; i < attributes.getSize(); ++i ) {
			names[i] = attributes[i]->getName();
		}

		return (names);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	String DomElementNode::getAttributeValue( String attribute ) const
	{
		Array<DomAttributeNode*> attributes = getAttributeNodes();

		for( int i = 0; i < attributes.getSize(); ++i ) {
			if( attributes[i]->getName() == attribute ) {
				return (attributes[i]->getValue());
			}
		}

		return (String::null);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	Array<String> DomElementNode::getTextValues() const
	{
		Array<DomTextNode*> textNodes = getTextNodes();
		Array<String> values(textNodes.getSize());

		for( int i = 0; i < textNodes.getSize(); ++i ) {
			values[i] = textNodes[i]->getValue();
		}

		return (values);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	String DomElementNode::getTextValue() const
	{
		Array<String> values = getTextValues();
		String result;

		for( int i = 0; i < values.getSize(); ++i ) {
			result += values[i];
		}

		return (result);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	void DomElementNode::setName( String name )
	{
		m_name = name;
	}

	// ---------------------------------------------------------------------------------------------------------------------

	void DomElementNode::setValue( String value )
	{
		if( getTextNodes().getSize() > 0 ) {
			setText(value, 0);
		}
		else {
			addText(value);
		}
	}

	// ---------------------------------------------------------------------------------------------------------------------

	void DomElementNode::setAttribute( String attribute, String value )
	{
		if( DomAttributeNode* attrNode = getAttributeNode(attribute) ) {
			attrNode->setValue(value);
		}
		else {
			new DomAttributeNode(this, attribute, value);
		}
	}

	// ---------------------------------------------------------------------------------------------------------------------

	void DomElementNode::addText( String text )
	{
		new DomTextNode(this, text);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	void DomElementNode::setText( String text, int textNode )
	{
		Array<DomTextNode*> textNodes = getTextNodes();
		if( textNode < 0 || textNode >= textNodes.getSize() ) {
			throw DomNodeException($("Text node index is out of range"));
		}

		textNodes[textNode]->setValue(text);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	DomNode* DomElementNode::clone( DomNode* parent )
	{
		return (new DomElementNode(parent, m_name));
	}


	// ---------------------------------------------------------------------------------------------------------------------
	// ---------------------------------------------------------------------------------------------------------------------
	// ---------------------------------------------------------------------------------------------------------------------
	// ---------------------------------------------------------------------------------------------------------------------
	// ---------------------------------------------------------------------------------------------------------------------


	DomAttributeNode::DomAttributeNode( DomNode* parent, String name, String value )
		:DomNode(parent), m_name(name), m_value(value)
	{
		if( parent == 0 || (parent->getNodeType() != DomNode::ELEMENT && parent->getNodeType() != DomNode::PROC_INST) ) {
			throw DomNodeException($("An attribute node must have a parent element or processing instruction node"));
		}
	}

	// ---------------------------------------------------------------------------------------------------------------------

	DomAttributeNode::~DomAttributeNode()
	{
	}

	// ---------------------------------------------------------------------------------------------------------------------

	String DomAttributeNode::getName() const
	{
		return (m_name);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	String DomAttributeNode::getValue() const
	{
		return (m_value);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	DomNode::node_type_e DomAttributeNode::getNodeType() const
	{
		return (DomNode::ATTRIBUTE);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	String DomAttributeNode::getPath() const
	{
		return (getParent()->getPath() + "/attribute::" + getName());
	}

	// ---------------------------------------------------------------------------------------------------------------------

	void DomAttributeNode::setName( String name )
	{
		m_name = name;
	}

	// ---------------------------------------------------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -