本文整理汇总了Java中org.jdom2.input.SAXBuilder.build方法的典型用法代码示例。如果您正苦于以下问题:Java SAXBuilder.build方法的具体用法?Java SAXBuilder.build怎么用?Java SAXBuilder.build使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jdom2.input.SAXBuilder
的用法示例。
在下文中一共展示了SAXBuilder.build方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDocumentFromString
import org.jdom2.input.SAXBuilder; //导入方法依赖的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.input.SAXBuilder; //导入方法依赖的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: parseXml
import org.jdom2.input.SAXBuilder; //导入方法依赖的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());
}
}
示例4: doGetPost
import org.jdom2.input.SAXBuilder; //导入方法依赖的package包/类
public void doGetPost(MCRServletJob job) throws Exception {
HttpServletRequest req = job.getRequest();
String path = job.getRequest().getPathInfo().substring(1);
LOGGER.info(path);
boolean b = tryLogin(path.substring(0, 6), path.substring(6), false);
if (b) {
org.apache.shiro.subject.Subject currentUser = SecurityUtils.getSubject();
if (currentUser.hasRole("client")) {
String hash = req.getUserPrincipal().getName();
File reportFile = new File(resultsDir + "/" + hash.substring(0, 6), "report.xml");
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(reportFile);
getLayoutService().doLayout(job.getRequest(), job.getResponse(), new MCRJDOMContent(document));
}
}
}
示例5: testAttributeOrdering
import org.jdom2.input.SAXBuilder; //导入方法依赖的package包/类
/**
* Tests if the Attribute order is preserved while parsing
*
* @author sholzer (04.05.2015)
* @throws IOException
* when file not found (i guess...)
* @throws JDOMException
* when something else goes wrong
*/
@Test
public void testAttributeOrdering() throws JDOMException, IOException {
String path = "src/test/resources/";
String filePath = path + "ConceptTest.xml";
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(filePath);
Element element = doc.getRootElement();
List<org.jdom2.Attribute> attributes = element.getAttributes();
System.out.println(attributes);
assertTrue(attributes.toString(), attributes.get(0).getName().equals("b"));
assertTrue(attributes.toString(), attributes.get(1).getName().equals("c"));
assertTrue(attributes.toString(), attributes.get(2).getName().equals("a"));
}
示例6: getUsersFromXML
import org.jdom2.input.SAXBuilder; //导入方法依赖的package包/类
public ArrayList<User> getUsersFromXML () throws JDOMException, IOException {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ArrayList<User> users = new ArrayList<User>();
SAXBuilder saxBuilder = new SAXBuilder();
File inputFile = new File(ec.getRealPath("/") + "WEB-INF\\users.xml");
Document document = saxBuilder.build(inputFile);
Element root = document.getRootElement();
List<Element> usersList = root.getChildren();
for (Element user : usersList) {
users.add(new User(
Integer.valueOf(user.getChild("id").getText()),
user.getChild("username").getText(),
user.getChild("password").getText()
));
}
return users;
}
示例7: getProductsFromXML
import org.jdom2.input.SAXBuilder; //导入方法依赖的package包/类
public List<Product> getProductsFromXML () throws JDOMException, IOException {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
List<Product> products = new ArrayList<Product>();
SAXBuilder saxBuilder = new SAXBuilder();
File inputFile = new File(ec.getRealPath("/") + "WEB-INF\\products.xml");
Document document = saxBuilder.build(inputFile);
Element root = document.getRootElement();
List<Element> productsList = root.getChildren();
for (Element product : productsList) {
products.add(new Product(
product.getChild("name").getText(),
product.getChild("link").getText(),
product.getChild("description").getText()
));
}
return products;
}
示例8: getMonomerList
import org.jdom2.input.SAXBuilder; //导入方法依赖的package包/类
public static List<Monomer> getMonomerList(String monomerXMLString)
throws JDOMException, IOException, MonomerException, CTKException, ChemistryException {
List<Monomer> l = new ArrayList<Monomer>();
if (null != monomerXMLString && monomerXMLString.length() > 0) {
SAXBuilder builder = new SAXBuilder();
ByteArrayInputStream bais = new ByteArrayInputStream(
monomerXMLString.getBytes());
Document doc = builder.build(bais);
Element root = doc.getRootElement();
List monomers = root.getChildren();
Iterator it = monomers.iterator();
while (it.hasNext()) {
Element monomer = (Element) it.next();
Monomer m = getMonomer(monomer);
if (MonomerParser.validateMonomer(m)) {
l.add(m);
}
}
}
return l;
}
示例9: DBConfig
import org.jdom2.input.SAXBuilder; //导入方法依赖的package包/类
/**
* 在初始化时从文件中获取配置并保存
*/
public DBConfig() {
SAXBuilder jdomBuilder = new SAXBuilder();
try {
String configPath = DBConfig.class.getResource("/")
+ "../../config/database-config.xml";
Document document = jdomBuilder.build(configPath);
Element root = document.getRootElement();
setDriver(root.getChildText("driver").trim());
setHost(root.getChildText("host").trim());
setPort(root.getChildText("port").trim());
setUsername(root.getChildText("username").trim());
setPassword(root.getChildText("password").trim());
setName(root.getChildText("name").trim());
} catch (JDOMException | IOException e) {
e.printStackTrace();
}
}
示例10: addSection
import org.jdom2.input.SAXBuilder; //导入方法依赖的package包/类
/**
* Adds a section to the MyCoRe webpage.
*
* @param title the title of the section
* @param xmlAsString xml string which is added to the section
* @param lang the language of the section specified by a language key.
* @return added section
*/
public Element addSection(String title, String xmlAsString, String lang) throws IOException, SAXParseException,
JDOMException {
String sb = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<!DOCTYPE MyCoReWebPage PUBLIC \"-//MYCORE//DTD MYCOREWEBPAGE 1.0//DE\" "
+ "\"http://www.mycore.org/mycorewebpage.dtd\">" + "<MyCoReWebPage>" + xmlAsString + "</MyCoReWebPage>";
SAXBuilder saxBuilder = new SAXBuilder();
saxBuilder.setEntityResolver((publicId, systemId) -> {
String resource = systemId.substring(systemId.lastIndexOf("/"));
InputStream is = getClass().getResourceAsStream(resource);
if (is == null) {
throw new IOException(new FileNotFoundException("Unable to locate resource " + resource));
}
return new InputSource(is);
});
StringReader reader = new StringReader(sb);
Document doc = saxBuilder.build(reader);
return this.addSection(title, doc.getRootElement().cloneContent(), lang);
}
示例11: jsonSerialize
import org.jdom2.input.SAXBuilder; //导入方法依赖的package包/类
@Test
public void jsonSerialize() throws Exception {
// simple text
Element e = new Element("hallo").setText("Hallo Welt");
JsonObject json = MCRXMLHelper.jsonSerialize(e);
assertEquals("Hallo Welt", json.getAsJsonPrimitive("$text").getAsString());
// attribute
e = new Element("hallo").setAttribute("hallo", "welt");
json = MCRXMLHelper.jsonSerialize(e);
assertEquals("welt", json.getAsJsonPrimitive("_hallo").getAsString());
// complex world class test
URL world = MCRXMLHelperTest.class.getResource("/worldclass.xml");
SAXBuilder builder = new SAXBuilder();
Document worldDocument = builder.build(world.openStream());
json = MCRXMLHelper.jsonSerialize(worldDocument.getRootElement());
assertNotNull(json);
assertEquals("World", json.getAsJsonPrimitive("_ID").getAsString());
assertEquals("http://www.w3.org/2001/XMLSchema-instance", json.getAsJsonPrimitive("_xmlns:xsi").getAsString());
JsonObject deLabel = json.getAsJsonArray("label").get(0).getAsJsonObject();
assertEquals("de", deLabel.getAsJsonPrimitive("_xml:lang").getAsString());
assertEquals("Staaten", deLabel.getAsJsonPrimitive("_text").getAsString());
assertEquals(2, json.getAsJsonObject("categories").getAsJsonArray("category").size());
}
示例12: loadFile
import org.jdom2.input.SAXBuilder; //导入方法依赖的package包/类
private Document loadFile(Path path) {
if (Files.notExists(path) || !Files.isRegularFile(path)) {
String msg = "Path " + path + " is invalid.";
throw new IllegalArgumentException(msg);
} else {
SAXBuilder builder = new SAXBuilder();
builder.setIgnoringBoundaryWhitespace(true);
builder.setIgnoringElementContentWhitespace(true);
builder.setJDOMFactory(new DefaultJDOMFactory());
try {
Document processAsDoc = builder.build(path.toFile());
if ("definitions".equals(processAsDoc.getRootElement().getName()) &&
BPMNNAMESPACE_STRING.equals(processAsDoc.getRootElement().getNamespaceURI())) {
return processAsDoc;
} else {
throw new IllegalArgumentException("File is not a valid BPMN file.");
}
} catch (JDOMException | IOException e) {
LOGGER.error("File could not be processed.", e);
throw new IllegalArgumentException("File is not a valid BPMN file.");
}
}
}
示例13: CarPark
import org.jdom2.input.SAXBuilder; //导入方法依赖的package包/类
public CarPark(String filename) {
Document doc = null;
SAXBuilder builder = new SAXBuilder();
try {
doc = (Document) builder.build(filename);
} catch (JDOMException | IOException e) {
e.printStackTrace();
}
name = getName(doc);
boundary = getBBox(doc);
Map<Integer, GeoNode> nodes = getNodes(doc);
Map<Integer, Way> ways = getWays(doc, nodes);
List<Storey> storeies = getStoreies(doc, ways);
calibBoxes = getCalibBoxes(doc, nodes);
addAll(storeies);
Collections.sort(this, Collections.reverseOrder());
}
示例14: loadAttackPathsFromFile
import org.jdom2.input.SAXBuilder; //导入方法依赖的package包/类
public static List<AttackPath> loadAttackPathsFromFile(String attackPathsFilePath, AttackGraph relatedAttackGraph) throws Exception {
FileInputStream file = new FileInputStream(attackPathsFilePath);
SAXBuilder sxb = new SAXBuilder();
Document document = sxb.build(file);
Element root = document.getRootElement();
List<AttackPath> result = new ArrayList<AttackPath>();
List<Element> attackPathsElements = root.getChildren("attack_path");
if (!attackPathsElements.isEmpty()) {
for (Element attackPathElement : attackPathsElements) {
if (attackPathElement != null) {
AttackPath attackPath = new AttackPath();
attackPath.loadFromDomElementAndAttackGraph(attackPathElement, relatedAttackGraph);
result.add(attackPath);
}
}
}
sortAttackPaths(result);
return result;
}
示例15: parse
import org.jdom2.input.SAXBuilder; //导入方法依赖的package包/类
/**
* Parse feed from input source with base location set and create channel.
*
* @param cBuilder specific channel builder to use.
* @param inpSource input source of data.
* @param baseLocation base location of feed.
* @return parsed channel.
* @throws IOException if IO errors occur.
* @throws ParseException if parsing is not possible.
*/
public static ChannelIF parse(ChannelBuilderIF cBuilder, InputSource inpSource,
URL baseLocation)
throws IOException, ParseException {
// document reading without validation
SAXBuilder saxBuilder = new SAXBuilder();
// turn off DTD loading
saxBuilder.setEntityResolver(new NoOpEntityResolver());
try {
Document doc = saxBuilder.build(inpSource);
ChannelIF channel = parse(cBuilder, doc);
channel.setLocation(baseLocation);
return channel;
} catch (JDOMException e) {
throw new ParseException("Problem parsing " + inpSource + ": " + e);
}
}