本文整理汇总了Java中org.jdom2.Document类的典型用法代码示例。如果您正苦于以下问题:Java Document类的具体用法?Java Document怎么用?Java Document使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Document类属于org.jdom2包,在下文中一共展示了Document类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDocumentFromString
import org.jdom2.Document; //导入依赖的package包/类
/**
* Create a JDOM document from an XML string.
*
* @param string
* @param encoding
* @return
* @throws IOException
* @throws JDOMException
* @should build document correctly
*/
public static Document getDocumentFromString(String string, String encoding) throws JDOMException, IOException {
if (encoding == null) {
encoding = DEFAULT_ENCODING;
}
byte[] byteArray = null;
try {
byteArray = string.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
}
ByteArrayInputStream baos = new ByteArrayInputStream(byteArray);
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(baos);
return document;
}
示例2: getDocumentFromString
import org.jdom2.Document; //导入依赖的package包/类
/**
* Create a JDOM document from an XML string.
*
* @param string
* @return
* @throws IOException
* @throws JDOMException
* @should build document correctly
*/
public static Document getDocumentFromString(String string, String encoding) throws JDOMException, IOException {
if (string == null) {
throw new IllegalArgumentException("string may not be null");
}
if (encoding == null) {
encoding = "UTF-8";
}
byte[] byteArray = null;
try {
byteArray = string.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
}
ByteArrayInputStream baos = new ByteArrayInputStream(byteArray);
// Reader reader = new StringReader(hOCRText);
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(baos);
return document;
}
示例3: exportXML
import org.jdom2.Document; //导入依赖的package包/类
public void exportXML(ArrayList<Pokemon> pokemons,String tipo){
try {
Element pokemon = new Element("pokemon");
Document doc = new Document(pokemon);
for(Pokemon p:pokemons) {
Element poke = new Element("pokemon");
poke.addContent(new Element("nombre").setText(p.getName()));
poke.addContent(new Element("peso").setText(String.valueOf(p.getWeight())));
poke.addContent(new Element("altura").setText(String.valueOf(p.getHeight())));
poke.addContent(new Element("base_experience").setText(String.valueOf(p.getBaseExperience())));
doc.getRootElement().addContent(poke);
}
XMLOutputter output = new XMLOutputter();
output.setFormat(Format.getPrettyFormat());
output.output(doc, new FileWriter("pokemons-tipo-"+tipo+".xml"));
}catch(Exception e) {
System.out.println("Ocurri� alg�n error");
}
}
示例4: parseXml
import org.jdom2.Document; //导入依赖的package包/类
public void parseXml(String fileName){
SAXBuilder builder = new SAXBuilder();
File file = new File(fileName);
try {
Document document = (Document) builder.build(file);
Element rootNode = document.getRootElement();
List list = rootNode.getChildren("author");
for (int i = 0; i < list.size(); i++) {
Element node = (Element) list.get(i);
System.out.println("First Name : " + node.getChildText("firstname"));
System.out.println("Last Name : " + node.getChildText("lastname"));
}
} catch (IOException io) {
System.out.println(io.getMessage());
} catch (JDOMException jdomex) {
System.out.println(jdomex.getMessage());
}
}
示例5: parse
import org.jdom2.Document; //导入依赖的package包/类
@Override
public SpawnModule parse(MapModuleContext context, Logger logger, Document doc) throws InvalidXMLException {
final SpawnParser parser = spawnParser.get();
List<Spawn> spawns = Lists.newArrayList();
List<Kit> playerKits = new ArrayList<>();
for(Element spawnsEl : doc.getRootElement().getChildren("spawns")) {
spawns.addAll(parser.parseChildren(spawnsEl, new SpawnAttributes()));
final Kit playerKit = context.needModule(KitParser.class).property(spawnsEl, "player-kit").optional(null);
if(playerKit != null) {
playerKits.add(playerKit);
}
}
if(parser.getDefaultSpawn() == null) {
throw new InvalidXMLException("map must have a single default spawn", doc);
}
return new SpawnModule(parser.getDefaultSpawn(), spawns, playerKits, parseRespawnOptions(context, logger, doc));
}
示例6: parse
import org.jdom2.Document; //导入依赖的package包/类
public static MobsModule parse(MapModuleContext context, Logger logger, Document doc) throws InvalidXMLException {
FilterParser filterParser = context.needModule(FilterParser.class);
Element mobsEl = doc.getRootElement().getChild("mobs");
Filter mobsFilter = StaticFilter.DENY;
if(mobsEl != null) {
if(context.getProto().isNoOlderThan(ProtoVersions.FILTER_FEATURES)) {
mobsFilter = filterParser.parseProperty(mobsEl, "filter");
} else {
Element filterEl = XMLUtils.getUniqueChild(mobsEl, "filter");
if(filterEl != null) {
mobsFilter = filterParser.parseElement(filterEl);
}
}
}
return new MobsModule(mobsFilter);
}
示例7: parse
import org.jdom2.Document; //导入依赖的package包/类
public static ItemDestroyModule parse(MapModuleContext context, Logger logger, Document doc) throws InvalidXMLException {
final Set<MaterialPattern> patterns = new HashSet<>();
for(Node itemRemoveNode : Node.fromChildren(doc.getRootElement(), "item-remove", "itemremove")) {
for(Node itemNode : Node.fromChildren(itemRemoveNode.asElement(), "item")) {
final MaterialPattern pattern = XMLUtils.parseMaterialPattern(itemNode);
patterns.add(pattern);
if(pattern.matches(Material.POTION)) {
// TODO: remove this after we update the maps
patterns.add(new MaterialPattern(Material.SPLASH_POTION));
}
}
}
if(patterns.isEmpty()) {
return null;
} else {
return new ItemDestroyModule(patterns);
}
}
示例8: readRootDocument
import org.jdom2.Document; //导入依赖的package包/类
public Document readRootDocument(Path file) throws InvalidXMLException {
checkNotNull(file, "file");
this.includeStack.clear();
Document result = this.readDocument(file);
this.includeStack.clear();
if(source.globalIncludes()) {
for(Path globalInclude : mapConfiguration.globalIncludes()) {
final Path includePath = findIncludeFile(null, globalInclude, null);
if(includePath != null) {
result.getRootElement().addContent(0, readIncludedDocument(includePath, null));
}
}
}
return result;
}
示例9: parse
import org.jdom2.Document; //导入依赖的package包/类
@Override
public @Nullable TeamModule parse(MapModuleContext context, Logger logger, Document doc) throws InvalidXMLException {
final List<TeamFactory> teams = teamsProvider.get();
if(teams.isEmpty()) return null;
final Map<String, TeamFactory> byName = new HashMap<>();
for(TeamFactory team : teams) {
final String name = team.getDefaultName();
final TeamFactory dupe = byName.put(name, team);
if(dupe != null) {
String msg = "Duplicate team name '" + name + "'";
final Element dupeNode = context.features().definitionNode(dupe);
if(dupeNode != null) {
msg += " (other team defined by " + Node.of(dupeNode).describeWithLocation() + ")";
}
throw new InvalidXMLException(msg, context.features().definitionNode(team));
}
}
Optional<Boolean> requireEven = Optional.empty();
for(Element elTeam : XMLUtils.flattenElements(doc.getRootElement(), "teams", "team")) {
requireEven = Optionals.first(XMLUtils.parseBoolean(elTeam, "even").optional(), requireEven);
}
return new TeamModule(teams, requireEven);
}
示例10: parse
import org.jdom2.Document; //导入依赖的package包/类
@Override
public PickupModule parse(MapModuleContext context, Logger logger, Document doc) throws InvalidXMLException {
KitParser kitParser = context.needModule(KitParser.class);
FilterParser filterParser = context.needModule(FilterParser.class);
RegionParser regionParser = context.needModule(RegionParser.class);
for(Element el : XMLUtils.flattenElements(doc.getRootElement(), "pickups", "pickup")) {
String name = el.getAttributeValue("name");
EntityType appearance = XMLUtils.parseEnum(Node.fromAttr(el, "appearance"), EntityType.class, "entity type", EntityType.ENDER_CRYSTAL);
if(appearance != EntityType.ENDER_CRYSTAL) {
throw new InvalidXMLException("Only ender crystal appearances are supported right now", el);
}
Filter visible = filterParser.property(el, "spawn-filter").optional(StaticFilter.ALLOW);
Filter pickup = filterParser.property(el, "pickup-filter").optional(StaticFilter.ALLOW);
Region region = regionParser.property(el, "region").validate(RandomPointsValidation.INSTANCE).required();
Kit kit = kitParser.property(el, "kit").optional(KitNode.EMPTY);
Duration refresh = XMLUtils.parseDuration(Node.fromAttr(el, "respawn-time"), Duration.ofSeconds(3));
Duration cooldown = XMLUtils.parseDuration(Node.fromAttr(el, "pickup-time"), Duration.ofSeconds(3));
boolean effects = XMLUtils.parseBoolean(Node.fromAttr(el, "effects"), true);
boolean sounds = XMLUtils.parseBoolean(Node.fromAttr(el, "sounds"), true);
context.features().define(el, new PickupDefinitionImpl(name, appearance, visible, pickup, region, kit, refresh, cooldown, effects, sounds));
}
return null;
}
示例11: readAbbyyToAlto
import org.jdom2.Document; //导入依赖的package包/类
/**
* Extracts page width and height attributes from the given ABBYY XML file and converts the document to ALTO.
*
* @param file
* @return
* @throws FileNotFoundException
* @throws IOException
* @throws JDOMException
* @throws XMLStreamException
* @throws FatalIndexerException
* @should convert to ALTO correctly
* @should throw IOException given wrong document format
*/
public static Map<String, Object> readAbbyyToAlto(File file) throws FileNotFoundException, IOException, XMLStreamException,
FatalIndexerException {
logger.trace("readAbbyy: {}", file.getAbsolutePath());
if (!FileFormat.ABBYYXML.equals(JDomXP.determineFileFormat(file))) {
throw new IOException(file.getAbsolutePath() + " is not a valid ABBYY XML document.");
}
Map<String, Object> ret = new HashMap<>();
// Convert to ALTO
Element alto = new ConvertAbbyyToAltoStaX().convert(file, new Date(file.lastModified()));
if (alto != null) {
Document altoDoc = new Document();
altoDoc.setRootElement(alto);
ret = readAltoDoc(altoDoc, file.getAbsolutePath());
logger.debug("Converted ABBYY XML to ALTO: {}", file.getName());
}
return ret;
}
示例12: checkSolrSchemaName
import org.jdom2.Document; //导入依赖的package包/类
private static boolean checkSolrSchemaName(Document doc) {
if (doc != null) {
Element eleRoot = doc.getRootElement();
if (eleRoot != null) {
String schemaName = eleRoot.getAttributeValue("name");
if (StringUtils.isNotEmpty(schemaName)) {
try {
if (schemaName.length() > SCHEMA_VERSION_PREFIX.length() && Integer.parseInt(schemaName.substring(SCHEMA_VERSION_PREFIX
.length())) >= SolrIndexerDaemon.MIN_SCHEMA_VERSION) {
return true;
}
} catch (NumberFormatException e) {
logger.error("Schema version must contain a number.");
}
logger.error("Solr schema is not up to date; required: {}{}, found: {}", SCHEMA_VERSION_PREFIX, MIN_SCHEMA_VERSION, schemaName);
}
}
} else {
logger.error("Could not read the Solr schema name.");
}
return false;
}
示例13: parse
import org.jdom2.Document; //导入依赖的package包/类
@Override
public @Nullable ProjectileModule parse(MapModuleContext context, Logger logger, Document doc) throws InvalidXMLException {
final ItemParser itemParser = context.needModule(ItemParser.class);
FilterParser filterParser = context.needModule(FilterParser.class);
for(Element projectileElement : XMLUtils.flattenElements(doc.getRootElement(), "projectiles", "projectile")) {
String name = projectileElement.getAttributeValue("name");
Double damage = XMLUtils.parseNumber(projectileElement.getAttribute("damage"), Double.class, (Double) null);
double velocity = XMLUtils.parseNumber(Node.fromChildOrAttr(projectileElement, "velocity"), Double.class, 1.0);
ClickAction clickAction = XMLUtils.parseEnum(Node.fromAttr(projectileElement, "click"), ClickAction.class, "click action", ClickAction.BOTH);
Class<? extends Entity> entity = XMLUtils.parseEntityTypeAttribute(projectileElement, "projectile", Arrow.class);
List<PotionEffect> potionKit = itemParser.parsePotionEffects(projectileElement);
Filter destroyFilter = filterParser.parseOptionalProperty(projectileElement, "destroy-filter").orElse(null);
Duration coolDown = XMLUtils.parseDuration(projectileElement.getAttribute("cooldown"));
boolean throwable = XMLUtils.parseBoolean(projectileElement.getAttribute("throwable"), true);
context.features().define(projectileElement, new ProjectileDefinitionImpl(name, damage, velocity, clickAction, entity, potionKit, destroyFilter, coolDown, throwable));
}
return null;
}
示例14: parse
import org.jdom2.Document; //导入依赖的package包/类
public static KillRewardModule parse(MapModuleContext context, Logger logger, Document doc) throws InvalidXMLException {
ImmutableList.Builder<KillReward> rewards = ImmutableList.builder();
final ItemParser itemParser = context.needModule(ItemParser.class);
final Optional<ItemModifyModule> itemModifier = context.module(ItemModifyModule.class);
// Must allow top-level children for legacy support
for(Element elKillReward : XMLUtils.flattenElements(doc.getRootElement(), ImmutableSet.of("kill-rewards"), ImmutableSet.of("kill-reward", "killreward"), 0)) {
ImmutableList.Builder<ItemStack> items = ImmutableList.builder();
for(Element itemEl : elKillReward.getChildren("item")) {
final ItemStack item = itemParser.parseItem(itemEl, false);
itemModifier.ifPresent(imm -> imm.applyRules(item));
items.add(item.immutableCopy());
}
Filter filter = context.needModule(FilterParser.class).property(elKillReward, "filter").optional(StaticFilter.ALLOW);
Kit kit = context.needModule(KitParser.class).property(elKillReward, "kit").optional(KitNode.EMPTY);
rewards.add(new KillReward(items.build(), filter, kit));
}
ImmutableList<KillReward> list = rewards.build();
if(list.isEmpty()) {
return null;
} else {
return new KillRewardModule(list);
}
}
示例15: parse
import org.jdom2.Document; //导入依赖的package包/类
@Override
public @Nullable ItemModifyModule parse(MapModuleContext context, Logger logger, Document doc) throws InvalidXMLException {
List<ItemRule> rules = new ArrayList<>();
for(Element elRule : XMLUtils.flattenElements(doc.getRootElement(), "item-mods", "rule")) {
MaterialMatcher items = XMLUtils.parseMaterialMatcher(XMLUtils.getRequiredUniqueChild(elRule, "match"));
// Always use a PotionMeta so the rule can have potion effects, though it will only apply those to potion items
final Element elModify = XMLUtils.getRequiredUniqueChild(elRule, "modify");
final PotionMeta meta = (PotionMeta) Bukkit.getItemFactory().getItemMeta(Material.POTION);
context.needModule(ItemParser.class).parseItemMeta(elModify, meta);
final boolean defaultAttributes = XMLUtils.parseBoolean(elModify.getAttribute("default-attributes"), false);
ItemRule rule = new ItemRule(items, meta, defaultAttributes);
rules.add(rule);
}
return rules.isEmpty() ? null : new ItemModifyModule(rules);
}