KXML入门实例

1.选定文件,取名example1.xml,内容如下:

<root>
  <foo>bar</foo>
  <hugo xmlns="http://hugospace.org">
    <hugochild>This is in a <!-- comment -->new namespace</hugochild>
  </hugo>
  <bar testattr='123abc' />
</root>

2.建立java类,如下:

import java.io.*;
import org.kxml2.kdom.*;
import org.kxml2.io.*;
import org.xmlpull.v1.*;

/**
 * @author Stefan Haustein
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 */

public class KdomTest {

 /**
  * Constructor for KDomRoundtrip.
  */
 
   
 public static void main(String[] args) throws IOException, XmlPullParserException {
  KdomTest kdom = new KdomTest();
  
   System.out.println ("generating KDom from ");
   
   KXmlParser parser = new KXmlParser ();
   parser.setInput(new InputStreamReader(kdom.getClass().getResourceAsStream("example1.xml")));
   parser.setFeature (XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
   
   Document doc = new Document ();
   
   doc.parse (parser);
            Element rootElement = doc.getRootElement();
            System.out.println("**********root name*************"+rootElement.getName()+"****child count:"+rootElement.getChildCount()+"***text:"+rootElement.getText(0));
            Element child1 = (Element)rootElement.getChild(1);
            System.out.println("child1***name"+child1.getName()+"***text:"+child1.getText(0));
           
            Element child2 = (Element)rootElement.getChild(3);
            System.out.println("child2***name"+child2.getName()+"**namespace:"+child2.getNamespace()+"**attribute count"+child2.getAttributeCount()+"**text:"+child2.getText(0));
   KXmlSerializer serializer = new KXmlSerializer ();
   serializer.setOutput (System.out, null);
   
   doc.write (serializer);
   serializer.flush ();
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值