jdom使用入门及namespace注意事项【原】

 报文样例

<person:info xmlns:person="http://person/abc" id="1">

<fruit id="2">
    <apple id="3" color="red" size="big" >good apple</apple>
</fruit>

    <person:pets id="4">

        <cat:pet xmlns:cat="http://cat/def" id="5" type="cat">
            <person:master id="6">King</person:master>
            <cat:age id="7">1</cat:age>
            <cat:color id="8">white</cat:color>
            <happy id="9">true</happy>
        </cat:pet>

        <dog:pet xmlns:dog="http://dog/ghi" id="10" type="dog">
            <person:master id="11">King</person:master>
            <dog:age id="12">2</dog:age>
            <dog:color id="13">white</dog:color>
            <happy id="14">false</happy>
        </dog:pet>

    </person:pets>
    
</person:info>

jdom样例

package d;

import java.io.File;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class JdomDemo {
    
    @SuppressWarnings({ "unused", "unchecked" })
    public static void main(String[] args) throws Exception{
        File file = new File("C:/Users/35992/Desktop/a.xml");
        Document doc =  (new SAXBuilder()).build(file); 
        Element root = doc.getRootElement();
        Namespace rootNS = root.getNamespace();
        
        Element fruit = root.getChild("fruit");//取单个直接孩子节点
        Element apple =fruit.getChild("apple");//取单个直接孩子节点
        List<Attribute> attrList =  apple.getAttributes();
        for(int i = 0 ; i < attrList.size() ; i++){//遍历apple 的3个属性
            String appleAttrValue = attrList.get(i).getValue();
            System.out.println(appleAttrValue);
        }
        
        //<person:pets id="4"> 有冒号需要添加person的Namespace,不添加会找不到,返回null
        Element petNull = root.getChild("pets");
        //<person:pets id="4"> 有冒号需要添加person的Namespace
        Element petNotNull = root.getChild("pets",Namespace.getNamespace("person", "http://person/abc"));
        
        Element pets = root.getChild("pets", rootNS);//取单个直接孩子节点
        
        List<Element> petList = pets.getChildren();//取所有直接孩子节点们
        for(int i = 0 ; i < petList.size(); i++){//pets有2个pet子节点
            Element pet = petList.get(i);
            
            if("cat".equals(pet.getAttributeValue("type"))){
                Element masterNull = pet.getChild("master");
                Element ageNull = pet.getChild("age");
                Element colorNull = pet.getChild("color");
                Element happy = pet.getChild("happy");
                System.out.println(masterNull+" - " + ageNull+" - " +  colorNull+" - " +  happy);
            }
                
            if("dog".equals(pet.getAttributeValue("type"))){
                //<person:master id="11">King</person:master> 有冒号需要添加person的Namespace
                Element master = pet.getChild("master",Namespace.getNamespace("person", "http://person/abc"));
                //<dog:age id="12">2</dog:age> 有冒号需要添加dog的Namespace
                Element age = pet.getChild("age",Namespace.getNamespace("dog", "http://dog/ghi"));
                //<dog:color id="13">white</dog:color> 有冒号需要添加dog的Namespace
                Element color = pet.getChild("color",Namespace.getNamespace("dog", "http://dog/ghi"));
                Element happy = pet.getChild("happy");//<happy id="14">false</happy> 没有冒号,直接取
                System.out.println(master+" - " + age+" - " +  color+" - " +  happy);
            }
        }
        
        //格式化输出xml文件字符串  
        Format format = Format.getCompactFormat().setEncoding("utf-8").setIndent("	");  
        XMLOutputter xmlout = new XMLOutputter(format);  
        String result = xmlout.outputString(doc);
        System.out.println(result);
    }

}

参考

XML 命名空间(XML Namespaces)--http://www.w3school.com.cn/xml/xml_namespaces.asp

原文地址:https://www.cnblogs.com/whatlonelytear/p/8565835.html