private Element elementFor(String content) throws JDOMException, IOException { SAXBuilder builder = new SAXBuilder(); return builder.build(new ByteArrayInputStream(content.getBytes())).getRootElement(); } }
private Document createEmptyCruiseConfigDocument() { Element root = new Element("cruise"); Namespace xsiNamespace = Namespace.getNamespace("xsi", XML_NS); root.addNamespaceDeclaration(xsiNamespace); registry.registerNamespacesInto(root); root.setAttribute("noNamespaceSchemaLocation", "cruise-config.xsd", xsiNamespace); String xsds = registry.xsds(); if (!xsds.isEmpty()) { root.setAttribute("schemaLocation", xsds, xsiNamespace); } root.setAttribute("schemaVersion", Integer.toString(GoConstants.CONFIG_SCHEMA_VERSION)); return new Document(root); }
@Override public void setRoot(Document doc, Element root) { doc.setRootElement(root); } }
public static void loadMacro(String folderPath) throws JDOMException, IOException{ File file = FileUtil.getFile(folderPath); // File file = new File(url.getFile()); if(!file.exists() || file.isFile()){ return; } folderPath = file.getPath(); String[] xmlFileNames = file.list(); for(String xmlFileName : xmlFileNames){ Document doc = new SAXBuilder().build(new File(folderPath,xmlFileName)); Element root = doc.getRootElement(); if(!"macros".equals(root.getName())){ continue; } for(Element macro : root.getChildren()){ new MacroObject(macro); } } }
public TableConfig readConfigXML(String fileLocation, FeatureType wantFeatureType, NetcdfDataset ds, Formatter errlog) throws IOException { org.jdom2.Document doc; try { SAXBuilder builder = new SAXBuilder(false); if (debugURL) System.out.println(" PointConfig URL = <" + fileLocation + ">"); doc = builder.build(fileLocation); } catch (JDOMException e) { throw new IOException(e.getMessage()); } if (debugXML) System.out.println(" SAXBuilder done"); if (showParsedXML) { XMLOutputter xmlOut = new XMLOutputter(); System.out.println("*** PointConfig/showParsedXML = \n" + xmlOut.outputString(doc) + "\n*******"); } Element configElem = doc.getRootElement(); String featureType = configElem.getAttributeValue("featureType"); Element tableElem = configElem.getChild("table"); TableConfig tc = parseTableConfig(ds, tableElem, null); tc.featureType = FeatureType.valueOf(featureType); return tc; }
private void writeOneEntry( InvDataset ds, OutputStream out, StringBuffer mess) throws IOException { Element rootElem = new Element("DIF", defNS); Document doc = new Document(rootElem); writeDataset( ds, rootElem, mess); rootElem.addNamespaceDeclaration(defNS); rootElem.addNamespaceDeclaration(XMLEntityResolver.xsiNS); rootElem.setAttribute("schemaLocation", defNS.getURI()+" "+schemaLocation, XMLEntityResolver.xsiNS); // Output the document, use standard formatter XMLOutputter fmt = new XMLOutputter( Format.getPrettyFormat()); fmt.output( doc, out); }
private InputStream convert2XmlDecoder(InputStream is) throws IOException { org.jdom2.Document doc; try { SAXBuilder builder = new SAXBuilder(false); doc = builder.build(is); } catch (JDOMException e) { throw new IOException(e.getMessage()); } finally { if (is != null) is.close(); } if (debugConvert) { XMLOutputter xmlOut = new XMLOutputter(); System.out.println("***XMLStore.convert2XmlDecoder = \n" + xmlOut.outputString(doc) + "\n*******"); } Element root = doc.getRootElement(); org.jdom2.Document convertDoc = new Document(); Element convertRoot = new Element("java"); convertRoot.setAttribute("version", "1.4.1_01"); convertRoot.setAttribute("class", "java.beans.XMLDecoder"); convertDoc.setRootElement(convertRoot); add(root, convertRoot); XMLOutputter xmlOut = new XMLOutputter(); xmlOut.setFormat(Format.getPrettyFormat()); if (debugConvert) { System.out.printf("%n************************************%n"); System.out.println("***Convert2Version2 converted = \n" + xmlOut.outputString(convertDoc) + "\n*******"); } return new StringBufferInputStream(xmlOut.outputString(convertDoc)); }
/** * Converts the navigation.xml to the old format. */ private static byte[] convertToOldFormat(byte[] xml) throws JDOMException, IOException { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new ByteArrayInputStream(xml)); Element rootElement = doc.getRootElement(); rootElement.setAttribute("href", rootElement.getName()); List<Element> children = rootElement.getChildren(); for (Element menu : children) { String id = menu.getAttributeValue("id"); menu.setName(id); menu.setAttribute("href", id); menu.removeAttribute("id"); } XMLOutputter out = new XMLOutputter(Format.getPrettyFormat()); ByteArrayOutputStream bout = new ByteArrayOutputStream(xml.length); out.output(doc, bout); return bout.toByteArray(); }
public GeniaPosParser(File xmlFile) throws IOException, JDOMException { this(); SAXBuilder builder = new SAXBuilder(); builder.setDTDHandler(null); root = builder.build(xmlFile).getRootElement(); articles = root.getChildren("article").iterator(); outputter = new XMLOutputter(); }
protected void writeXML(AvailableResources t, HttpOutputMessage outputMessage) throws IOException { Element root = new Element("list"); final Document doc = new Document(root); XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); t.stream().map(name -> new Element(t.getName()).addContent(name)).forEach(root::addContent); try (OutputStream os = outputMessage.getBody()) { outputter.output(doc, os); } } }
static public void passTwo() throws IOException { org.jdom2.Document tdoc; try { SAXBuilder builder = new SAXBuilder(); tdoc = builder.build(trans1); org.jdom2.Document ndoc = new org.jdom2.Document(); Element nroot = new Element("ndoc"); ndoc.setRootElement(nroot); transform2(tdoc.getRootElement(), nroot); XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat()); Writer pw = new FileWriter(trans2); fmt.output(ndoc, pw); pw = new PrintWriter(System.out); fmt.output(ndoc, pw); } catch (JDOMException e) { throw new IOException(e.getMessage()); } }
public void writeXMLexplicit(NetcdfFile ncfile, OutputStream os, String location) throws IOException { if (ncfile instanceof NetcdfDataset) ncd = (NetcdfDataset) ncfile; else ncd = new NetcdfDataset(ncfile, false); fmt = new XMLOutputter(Format.getPrettyFormat()); Document doc = makeDocument(location); Element root = doc.getRootElement(); root.addContent( new Element("explicit", ncNS)); fmt.output(doc, os); }
public HashMap<String, String> parseInfoToGetUUID(String output, String queryURL, SAXBuilder builder) { HashMap<String, String> uidToUrlMap = new HashMap<>(); try { Document document = builder.build(new StringReader(output)); Element root = document.getRootElement(); List<Element> entries = root.getChildren("entry"); for (Element entry : entries) { uidToUrlMap.put(queryURL, entry.getChild("repository").getChild("uuid").getValue()); } } catch (Exception e) { throw new RuntimeException(e); } return uidToUrlMap; }
@Test @Ignore("WMS not working") public void checkWMSDates() throws JDOMException, IOException { String endpoint = TestOnLocalServer.withHttpPath("/wms/cdmUnitTest/ncss/climatology/PF5_SST_Climatology_Monthly_1985_2001.nc?service=WMS&version=1.3.0&request=GetCapabilities"); byte[] result = TestOnLocalServer.getContent(endpoint, 200, ContentType.xml); Reader in = new StringReader( new String(result, CDM.utf8Charset)); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(in); if (show) { XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat()); fmt.output(doc, System.out); } XPath xPath = XPath.newInstance("//wms:Dimension"); xPath.addNamespace("wms", doc.getRootElement().getNamespaceURI()); Element dimNode = (Element) xPath.selectSingleNode(doc); //List<String> content = Arrays.asList(dimNode.getText().trim().split(",")); List<String> content = new ArrayList<>(); for (String d : Arrays.asList(dimNode.getText().trim().split(","))) { // System.out.printf("Date= %s%n", d); CalendarDate cd = CalendarDate.parseISOformat(null, d); content.add(cd.toString()); } assertEquals(expectedDatesAsDateTime, content); }
private int getCurrentSchemaVersion(String content) { try { SAXBuilder builder = new SAXBuilder(); Document document = builder.build(new ByteArrayInputStream(content.getBytes())); Element root = document.getRootElement(); String currentVersion = root.getAttributeValue(schemaVersion) == null ? "0" : root.getAttributeValue(schemaVersion); return Integer.parseInt(currentVersion); } catch (Exception e) { throw bomb(e); } }
private void writeXmlWithExcludes(InputStream inputStream, OutputStream outputStream, Set<String> namespacePrefixes) throws IOException, JDOMException { SAXBuilder saxBuilder = new SAXBuilder(); Document doc = saxBuilder.build(inputStream); Set<String> namespacePrefixesActuallyUsed = new HashSet<>(); applyXmlExcludes(doc.getRootElement(), "", namespacePrefixesActuallyUsed); XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat() .setIndent(" ") .setLineSeparator(LineSeparator.UNIX)); outputter.setXMLOutputProcessor(new OneAttributePerLineXmlProcessor(namespacePrefixes, namespacePrefixesActuallyUsed)); outputter.output(doc, outputStream); outputStream.flush(); }
static public void passOne() throws IOException { org.jdom2.Document orgDoc; try { SAXBuilder builder = new SAXBuilder(); orgDoc = builder.build(orgXml); org.jdom2.Document tdoc = new org.jdom2.Document(); Element root = new Element("tdoc"); tdoc.setRootElement(root); transform(orgDoc.getRootElement(), root); /* XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat()); Writer pw = new FileWriter("C:/docs/bufr/wmo/Code-FlagTables-11-2007.trans.xml"); fmt.output(tdoc, pw); pw = new PrintWriter(System.out); fmt.output(tdoc, pw); // */ } catch (JDOMException e) { throw new IOException(e.getMessage()); } }
@Test @Category(NeedsCdmUnitTest.class) public void testFeatureCollectionReader() throws IOException, JDOMException { File configFile = new File(TestDir.cdmUnitTestDir+"tds/config/fnmoc.xml"); org.jdom2.Document doc; SAXBuilder builder = new SAXBuilder(); doc = builder.build(configFile); XMLOutputter xmlOut = new XMLOutputter(); System.out.println(xmlOut.outputString(doc)); Formatter errlog = new Formatter(); FeatureCollectionConfigBuilder fcb = new FeatureCollectionConfigBuilder(errlog); FeatureCollectionConfig config = fcb.readConfig(doc.getRootElement()); DateExtractor extractor = config.getDateExtractor(); String path = "[DATA_DIR]/native/grid/FNMOC/COAMPS/FNMOC_COAMPS_Equatorial_America_20141207_1200.grib1.ncx3"; MFile mfile = new MFileOS7(Paths.get(path), null); CalendarDate cd = extractor.getCalendarDate(mfile); assert cd != null : "date extractor failed"; System.out.printf("%s -> %s%n", path, cd); assert(cd.toString().equals("2014-12-07T12:00:00Z")); }
private boolean isGetCoverageWcsDoc(String url) throws JDOMException, IOException { byte[] result = TestOnLocalServer.getContent(url+baloney+"&request=GetCapabilities", 200, ContentType.xml); Reader in = new StringReader( new String(result, CDM.utf8Charset)); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(in); boolean isName = doc.getRootElement().getName().equals("WCS_Capabilities"); boolean isNamespace = doc.getRootElement().getNamespaceURI().equals(NS_WCS.getURI()); return (isName && isNamespace); }
try SAXBuilder builder = new SAXBuilder( false ); builder.setExpandEntities( false ); if ( ignoreDtd ) builder.setEntityResolver( new EntityResolver() throw new RuntimeException( "Error processing resource " + resource + ": " + e.getMessage(), e ); appendElement( r.getRootElement(), doc.getRootElement() ); Object source = path.selectSingleNode( r.getRootElement() ); if ( !( source instanceof Element ) ) xpath, source ) ); Object target = path.selectSingleNode( doc.getRootElement() ); if ( !( target instanceof Element ) )