아래의 xml을 파싱하는 법이 여러가지 있는데 여기서는 jaxb로 파싱하는 법을 작성하려고 한다.
1 2 3 4 5 6 7 8 9 10 11 12
<GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resourcename="UserDatabase"auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources>
JAXBContextcontext= JAXBContext.newInstance(GlobalNamingResources.class); StringReaderreader=newStringReader("<GlobalNamingResources>\n" + " <Resource name=\"UserDatabase\" auth=\"Container\"\n" + "\t\ttype=\"org.apache.catalina.UserDatabase\"\n" + "\t\tdescription=\"User database that can be updated and saved\"\n" + "\t\tfactory=\"org.apache.catalina.users.MemoryUserDatabaseFactory\"\n" + "\t\tpathname=\"conf/tomcat-users.xml\" />\n" + " </GlobalNamingResources>"); GlobalNamingResourcesresources= (GlobalNamingResources) context.createUnmarshaller() .unmarshal(reader);
for (Resource fileInfo : resources.getResource()) { System.out.println(fileInfo); }
}
}
실행결과
1 2 3 4 5
Resource(name=UserDatabase, auth=Container, type=org.apache.catalina.UserDatabase, description=User database that can be updated and saved, factory=org.apache.catalina.users.MemoryUserDatabaseFactory, pathname=conf/tomcat-users.xml)