术_技_析_解_L_M_X

本文提供了一个使用Java进行XML文件解析与创建的详细示例。通过代码展示了如何利用DocumentBuilderFactory和DocumentBuilder来解析XML文件,并获取指定节点的数据;同时介绍了如何创建新的XML文件并保存到磁盘。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://liuyuru.iteye.com/blog/777531
http://www.iteye.com/topic/68166
InputStream in = getClass().getResourceAsStream("NewFile.xml");
//创建一个文档解析工厂实例
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//根据解析工厂创建一个解析器
DocumentBuilder db = dbf.newDocumentBuilder();
//用解析器将文档流转换成文档对象
Document doc = db.parse(in);
//根据文档对象查找根节点
Element root = doc.getDocumentElement();
NodeList nodeList = root.getChildNodes();
System.out.println(nodeList.getLength());
for (int i = 0; i < nodeList.getLength(); i++)
{
System.out.println(nodeList.item(i));
}
for (Node node = root.getFirstChild(); node != null; node = root.getNextSibling())
{
System.out.println("[节点名称:" + node.getNodeName() + ",节点类型:"
+ node.getNodeType() + "]");
}
--------------------------------------------------------------------
Element element = document.getDocumentElement();

//获取某个节点 person
NodeList nodeList = element.getElementsByTagName("person"); //$NON-NLS-1$
System.out.println("Length: " + nodeList.getLength());
for (int i = 0; i < nodeList.getLength(); i++)
{
//获取person节点下的单个属性
Element ele = (Element)nodeList.item(i);
System.out.println(ele.getAttribute("id")); //16347
System.out.println(ele.getAttributeNode("id")); //id="16347"

//获取person节点下的所有属性
NamedNodeMap map = ele.getAttributes();
for (int j = 0; j < map.getLength(); j++)
{
System.out.println("Name: " + map.item(j).getNodeName()
+ " Type: " + map.item(j).getNodeType() + " Value: "
+ map.item(j).getNodeValue());
}

NodeList nodes = ele.getChildNodes();
for (int j = 0; j < nodes.getLength(); j++)
{
//Node node = nodes.item(i).getNodeName();
// NamedNodeMap a = node.getAttributes();
// for (int k = 0; k < a.getLength(); k++)
// {
// System.out.println(a.item(k).getNodeName());
// }
System.out.println(":: " + nodes.item(i).getNodeValue());
}
}

=====================================================
/**
* 解析XML文件
*/
String xmlPath = "E:\\xbliuc.xml";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
Document parse = db.parse(xmlPath);
// NodeList nl = parse.getElementsByTagName("name");
// System.out.println(nl.item(0).getFirstChild().getNodeValue());
NodeList ss = parse.getElementsByTagName("student");
for (int i = 0; i < ss.getLength(); i++) {
Element e = (Element) ss.item(i);
String nameValue = e.getElementsByTagName("name").item(0)
.getFirstChild().getNodeValue();
String addressValue = e.getElementsByTagName("address").item(0)
.getFirstChild().getNodeValue();
System.out.println(nameValue + " " + addressValue);
}
}


/**
* 创建XML文件
*/
DocumentBuilderFactory bfd = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = bfd.newDocumentBuilder();
Document doc = db.newDocument();
Element school = doc.createElement("School");
Element student = doc.createElement("student");
Element name = doc.createElement("name");
Element address = doc.createElement("address");

// 创建文本节点
name.appendChild(doc.createTextNode("刘小彬"));
address.appendChild(doc.createTextNode("中国湖南"));
student.appendChild(name);
student.appendChild(address);
school.appendChild(student);
doc.appendChild(school);

// 输出
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
DOMSource dsourde = new DOMSource(doc);
StreamResult sResult = new StreamResult(new File("E:"
+ File.separator + "xbliuc2.xml"));
tf.transform(dsourde, sResult);

}
#include using namespace std; templateclass list { public: // 缺省构造 list() : m_head(NULL), m_tail(NULL) {} // 拷贝构造 list( const list& that ) : m_head(NULL),m_tail(NULL) { for( node* pnode=that.m_head; pnode!=NULL; pnode=pnode->m_next ) { push_back(pnode->m_data); } } // 构函数 ~list() { clear(); } // 链表判空 bool empty() const { return m_headNULL && m_tailNULL; } // 添加头节点 void push_front( const T& data ) { m_head = new node( NULL, data, m_head ); if( m_head->m_next ) m_head->m_next->m_prev = m_head; else m_tail = m_head; } // 添加尾节点 void push_back( const T& data ) { m_tail = new node( m_tail, data, NULL ); if( m_tail->m_prev ) m_tail->m_prev->m_next = m_tail; else m_head = m_tail; } // 删除头节点 void pop_front() throw(underflow_error) { if( empty() ) throw underflow_error(“null node”); node* pnext = m_head->m_next; delete m_head; if( pnext ) pnext->m_prev = NULL; else m_tail = NULL; m_head = pnext; } // 删除尾节点 void pop_back() { if( empty() ) throw underflow_error(“null node”); node* prev = m_tail->m_prev; delete m_tail; if( prev ) prev->m_next = NULL; else m_head = NULL; m_tail = prev; } // 获取头节点数据 T front() const throw(out_of_range) { if( empty() ) throw out_of_range(“null node”); return m_head->m_data; } // 获取尾节点数据 T back() const throw(out_of_range) { if( empty() ) throw out_of_range(“null node”); return m_tail->m_data; } // 链表清空 void clear() { while( !empty() ) { pop_front(); } } // 获取链表大小 size_t size() const { size_t i = 0; for( node* pnode=m_head; pnode!=NULL; pnode=pnode->m_next ) { i; } return i; } private: // 节点类 class node { public: node( node* prev, const T& data, node* next ) : m_prev(prev),m_data(data),m_next(next) {} node* m_prev; // 前指针 T m_data; node* m_next; // 后指针 }; public: // 非常迭代类(用于非常容器) class iterator { public: iterator( node* start, node* cur, node* end ) : m_start(start),m_cur(cur),m_end(end) {} iterator& operator() { if( m_cur == NULL ) m_cur = m_start; else m_cur = m_cur->m_next; return this; } iterator& operator–() { if( m_curNULL ) m_cur = m_end; else m_cur = m_cur->m_prev; return this; } T& operator() throw(out_of_range) { if( m_curNULL ) throw out_of_range(“null node”); return m_cur->m_data; } bool operator==( const iterator& that ) const { return m_startthat.m_start && m_curthat.m_cur && m_endthat.m_end; } bool operator!=( const iterator& that ) const { return !(*thisthat); } private: node m_start; // 开始指向 node* m_cur; // 当前指向 node* m_end; // 终止指向 friend class list; }; // 常迭代类(用于常容器) class const_iterator { public: const_iterator( node* start, node* cur, node* end ) : m_start(start),m_cur(cur),m_end(end) {} const_iterator& operator++() { if( m_cur == NULL ) m_cur = m_start; else m_cur = m_cur->m_next; return this; } const_iterator& operator–() { if( m_cur == NULL ) m_cur = m_end; else m_cur = m_cur->m_prev; return this; } const T& operator() throw(out_of_range) { // 重点体会为什么在这里加const if( m_curNULL ) throw out_of_range(“null node”); return m_cur->m_data; } bool operator( const const_iterator& that ) const { return m_startthat.m_start && m_curthat.m_cur && m_endthat.m_end; } bool operator!=( const const_iterator& that ) const { return !(*thisthat); } private: node m_start; node* m_cur; node* m_end; }; // 创建非常起始迭代器(用于遍历) iterator begin() { return iterator(m_head,m_head,m_tail); } // 创建非常终止迭代器(结束标识) iterator end() { return iterator(m_head,NULL,m_tail); } // 创建常起始迭代器(用于遍历) const_iterator begin() const { return const_iterator(m_head,m_head,m_tail); } // 创建常终止迭代器(结束标识) const_iterator end() const { return const_iterator(m_head,NULL,m_tail); } // 在迭代器指向的位置添加节点 void insert( const iterator& loc, const T& data ) { if( loc == end() ) { push_back( data ); } else { node* pnew = new node(loc.m_cur->m_prev, data, loc.m_cur ); if( pnew->m_prev ) pnew->m_prev->m_next = pnew; else m_head = pnew; pnew->m_next->m_prev = pnew; } } // 删除迭代器指向的节点 void erase( const iterator& loc ) { if( locend() ) throw out_of_range(“null node”); node* pdel = loc.m_cur; if( pdel->m_prev ) pdel->m_prev->m_next = pdel->m_next; else { pdel->m_next->m_prev = NULL; m_head = pdel->m_next; } if( pdel->m_next ) pdel->m_next->m_prev = pdel->m_prev; else { pdel->m_prev->m_next = NULL; m_tail = pdel->m_prev; } delete pdel; } private: node* m_head; // 链表头指针 node* m_tail; // 链表尾指针 }; // 利用"“比较查找 template<typename IT, typename T> IT find( const IT& beg, const IT& end, const T& data ) { for( IT it=beg; it!=end; ++it ) { if( *it==data ) { return it; } } return end; } // 利用”<"实现的排序 templatevoid sort( const IT& beg, const IT& end ) { IT last = end; –last; IT p = beg; for( IT i=beg, j=last; i!=j; ) { while( i!=p && *i<*p ) { ++i; } if( i!=p ) { swap( *i, *p ); } while( j!=p && *p<*j ) { –j; } if( j!=p ) { swap( *p, *j ); } } IT it = beg; ++it; if( p!=beg && p!=it ) { sort( beg, p ); } it = p; ++it; if( it!=end && it!=last ) { sort( it, end ); } } // 容器设计者提供比较类 templateclass Greater { public: bool operator()( T x, T y) { return x > y; } }; // 利用"比较器"实现的排序 template<typename IT, typename CMP>void sort( const IT& beg, const IT& end, CMP cmp ) { IT last = end; –last; IT p = beg; for( IT i=beg, j=last; i!=j; ) { while( i!=p && cmp(*i,*p) ) { // cmp.operator()(*i,*p) ++i; } if( i!=p ) { swap( *i, *p ); } while( j!=p && cmp(*p,*j) ) { // cmp.operator()(*p,*j) –j; } if( j!=p ) { swap( *p, *j ); } } IT it = beg; ++it; if( p!=beg && p!=it ) { sort( beg, p, cmp ); } it = p; ++it; if( it!=end && it!=last ) { sort( it, end, cmp ); } } // 以上代码容器的设计者 // ----------------------- // 以下代码容器的使用者 void Print( const string& str, const list& l ) { cout << str << endl; typedef list::const_iterator CIT; for( CIT it=l.begin(); it!=l.end(); ++it ) { cout << *it << ’ '; } cout << endl << “----------------------------” << endl; } // 用户设计比较类 class ZJW { public: bool operator()( int x, int y ) { return x < y; } }; int main( void ) { list ls; // 非常容器(非常对象) for( int i=0; i<5; i++ ) { ls.push_front(1000+i); } for( int i=0; i<5; i++ ) { ls.push_back(100+i); } Print( "添加头尾节点后:", ls ); ls.pop_front(); ls.pop_back(); Print( "删除头尾节点后:", ls ); ls.insert( ++ls.begin(), 888 ); // 增 Print( "迭代器指向的位置添加节点后:", ls ); ls.erase( ----ls.end() ); // 删 Print( "删除迭代器指向的节点后:", ls ); typedef list<int>::iterator IT; IT it = ls.begin(); *it = 999; // 改 Print( "修改迭代器指向的节点后:", ls ); IT fit = find( ls.begin(), ls.end(), 100 ); // 查 if( fit != ls.end() ) { ls.erase( fit ); } Print( "找到100并删除后:", ls ); // sort( ls.begin(), ls.end() ); // 利用<排序 // sort( ls.begin(), ls.end(), ZJW() ); // 利用 用户自己设计的比较器 Greater<int> g;// 容器设计者提供的比较器 sort( ls.begin(), ls.end(), g ); // 利用 容器设计者提供的比较器 Print( "排序后:", ls ); const list<int> cls = ls; // 常容器(常对象) Print( "常容器:", cls ); sort( cls.begin(), cls.end(), ZJW() ); Print( "排序后:", cls ); return 0; } 帮我注释这段代码,并且为我释里面迭代器有关的知识
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值