List of usage examples for org.w3c.dom Element setTextContent
public void setTextContent(String textContent) throws DOMException;
From source file:org.linguafranca.pwdb.kdbx.dom.DomHelper.java
@NotNull static Element setElementContent(String elementPath, Element parentElement, String value) { Element result = getElement(elementPath, parentElement, true); result.setTextContent(value); return result; }
From source file:org.mapsforge.directions.TurnByTurnDescriptionToString.java
/** * Creates a KML (Keyhole markup language) version of the directions. * /*from w ww .j av a2 s . c om*/ * @return a KML string * @throws ParserConfigurationException * if the DOM can't be built * @throws TransformerConfigurationException * if turning the DOM into a string fails * @throws TransformerException * if turning the DOM into a string fails * @throws TransformerFactoryConfigurationError * if turning the DOM into a string fails */ public String toKML() throws ParserConfigurationException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError { // This creates a new DOM Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); // And let's get this started dom.setXmlVersion("1.0"); dom.setXmlStandalone(true); Element kml = dom.createElement("kml"); dom.appendChild(kml); kml.setAttribute("xmlns", "http://www.opengis.net/kml/2.2"); Element document = dom.createElement("Document"); kml.appendChild(document); Element name = dom.createElement("name"); name.setTextContent( "MapsForge directions from " + streets.firstElement().name + " to " + streets.lastElement().name); document.appendChild(name); Element style = dom.createElement("Style"); style.setAttribute("id", "MapsForgeStyle"); document.appendChild(style); Element lineStyle = dom.createElement("LineStyle"); style.appendChild(lineStyle); Element color = dom.createElement("color"); color.setTextContent("ff0000ff"); lineStyle.appendChild(color); Element width = dom.createElement("width"); width.setTextContent("3"); lineStyle.appendChild(width); for (TurnByTurnStreet street : streets) { Element placemark = dom.createElement("Placemark"); document.appendChild(placemark); Element placemarkName = dom.createElement("name"); placemarkName.setTextContent(street.name); placemark.appendChild(placemarkName); Element lineString = dom.createElement("LineString"); placemark.appendChild(lineString); Element coordinates = dom.createElement("coordinates"); lineString.appendChild(coordinates); String coordinatesContent = ""; for (GeoCoordinate c : street.points) { coordinatesContent += c.getLongitude() + "," + c.getLatitude() + " "; } coordinatesContent = coordinatesContent.substring(0, coordinatesContent.length() - 1); // remove last space coordinates.setTextContent(coordinatesContent); Element extendedData = dom.createElement("ExtendedData"); placemark.appendChild(extendedData); Element length = dom.createElement("Length"); extendedData.appendChild(length); length.setTextContent(Double.toString(street.length)); Element angle = dom.createElement("AngleToPreviousStreet"); extendedData.appendChild(angle); angle.setTextContent(Double.toString(street.angleFromStreetLastStreet)); Element styleUrl = dom.createElement("styleUrl"); placemark.appendChild(styleUrl); styleUrl.setTextContent("#MapsForgeStyle"); } // This is for turning the DOM object into a proper StringWriter StringWriter stringWriter = new StringWriter(); TransformerFactory.newInstance().newTransformer().transform(new DOMSource(dom), new StreamResult(stringWriter)); return stringWriter.getBuffer().toString(); }
From source file:org.mapsforge.directions.TurnByTurnDescriptionToString.java
/** * Creates a GPX (GPS Exchange Format) version of the directions. * //from w w w. ja v a2s.c om * @return a KML string * @throws ParserConfigurationException * if the DOM can't be built * @throws TransformerConfigurationException * if turning the DOM into a string fails * @throws TransformerException * if turning the DOM into a string fails * @throws TransformerFactoryConfigurationError * if turning the DOM into a string fails */ public String toGPX() throws ParserConfigurationException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError { // This creates a new DOM Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); // And let's get this started dom.setXmlVersion("1.0"); dom.setXmlStandalone(true); Element gpx = dom.createElement("gpx"); dom.appendChild(gpx); gpx.setAttribute("version", "1.1"); gpx.setAttribute("xmlns", "http://www.topografix.com/GPX/1/1"); gpx.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); gpx.setAttribute("xmlns:mf", "http://tom.mapsforge.de"); gpx.setAttribute("xsi:schemaLocation", "http://www.topografix.com/GPX/1/1 http://www.topografix.com/gpx/1/1/gpx.xsd"); gpx.setAttribute("creator", "tom.mapsforge.de"); Element metadata = dom.createElement("metadata"); gpx.appendChild(metadata); Element name = dom.createElement("name"); name.setTextContent( "MapsForge directions from " + streets.firstElement().name + " to " + streets.lastElement().name); metadata.appendChild(name); for (TurnByTurnStreet street : streets) { Element trk = dom.createElement("trk"); gpx.appendChild(trk); Element trkName = dom.createElement("name"); trkName.setTextContent(street.name); trk.appendChild(trkName); Element trkseg = dom.createElement("trkseg"); trk.appendChild(trkseg); for (GeoCoordinate c : street.points) { Element trkpt = dom.createElement("trkpt"); trkseg.appendChild(trkpt); trkpt.setAttribute("lat", Double.toString(c.getLatitude())); trkpt.setAttribute("lon", Double.toString(c.getLongitude())); } Element extensions = dom.createElement("extensions"); trkseg.appendChild(extensions); Element length = dom.createElement("mf:Length"); extensions.appendChild(length); length.setTextContent(Double.toString(street.length)); Element angle = dom.createElement("mf:AngleToPreviousStreet"); extensions.appendChild(angle); angle.setTextContent(Double.toString(street.angleFromStreetLastStreet)); } // This is for turning the DOM object into a proper StringWriter StringWriter stringWriter = new StringWriter(); TransformerFactory.newInstance().newTransformer().transform(new DOMSource(dom), new StreamResult(stringWriter)); return stringWriter.getBuffer().toString(); }
From source file:org.minig.filters.FetchForward.java
@Override public void execute(IProxy p, IParameterSource req, IResponder responder) { logger.info("fetchVacation for user " + p.getAccount().getUserId()); Document doc = null;/* www. j av a 2 s . co m*/ try { doc = DOMUtils.createDoc("http://minig.org/xsd/forward.xsd", "forward"); SettingService ss = new SettingService(p.getAccount()); MinigForward mv = ss.getEmailForwarding(); Element root = doc.getDocumentElement(); root.setAttribute("enabled", "" + mv.isEnabled()); root.setAttribute("localCopy", "" + mv.isLocalCopy()); root.setAttribute("allowed", "" + mv.isAllowed()); root.setTextContent(mv.getEmail()); } catch (Exception e) { logger.error(e.getMessage(), e); } try { responder.sendDom(doc); } catch (Exception e1) { } }
From source file:org.minig.filters.FetchVacation.java
@Override public void execute(IProxy p, IParameterSource req, IResponder responder) { logger.info("fetchVacation for user " + p.getAccount().getUserId()); Document doc = null;//from w w w .java2 s . c o m try { doc = DOMUtils.createDoc("http://minig.org/xsd/vacation.xsd", "vacation"); SettingService ss = new SettingService(p.getAccount()); MinigVacation mv = ss.getVacationSettings(); Element root = doc.getDocumentElement(); root.setAttribute("enabled", "" + mv.isEnabled()); if (mv.getStart() != null) { root.setAttribute("start", "" + mv.getStart().getTime()); } if (mv.getEnd() != null) { root.setAttribute("end", "" + mv.getEnd().getTime()); } root.setTextContent(mv.getText()); } catch (Exception e) { logger.error(e.getMessage(), e); } try { responder.sendDom(doc); } catch (Exception e1) { } }
From source file:org.miradi.xml.TestXmpz2ForwardMigration.java
public void testRenameWhenAssignedFieldsMigration() throws Exception { getProject().createAndPopulateObjectTreeTableConfiguration(); Document document = convertProjectToDocument(); Element rootElement = document.getDocumentElement(); NodeList planningViewConfigurationColumnNamesContainer = rootElement .getElementsByTagName(PREFIX + Xmpz2XmlConstants.OBJECT_TREE_TABLE_CONFIGURATION + Xmpz2XmlConstants.COLUMN_CONFIGURATION_CODES + Xmpz2XmlConstants.CONTAINER_ELEMENT_TAG); for (int index = 0; index < planningViewConfigurationColumnNamesContainer.getLength(); ++index) { Node container = planningViewConfigurationColumnNamesContainer.item(index); Element codeWhenTotalNode = document.createElement(PREFIX + CODE_ELEMENT_NAME); codeWhenTotalNode.setTextContent(MigrationTo20.LEGACY_READABLE_ASSIGNED_WHEN_TOTAL_CODE); container.appendChild(codeWhenTotalNode); }//from w ww .j a v a2s. c om verifyMigratedXmpz2(document); }
From source file:org.miradi.xml.TestXmpz2ForwardMigration.java
public void testRemoveWhoAssignedFieldsMigration() throws Exception { getProject().createAndPopulateObjectTreeTableConfiguration(); Document document = convertProjectToDocument(); Element rootElement = document.getDocumentElement(); NodeList planningViewConfigurationColumnNamesContainer = rootElement .getElementsByTagName(PREFIX + Xmpz2XmlConstants.OBJECT_TREE_TABLE_CONFIGURATION + Xmpz2XmlConstants.COLUMN_CONFIGURATION_CODES + Xmpz2XmlConstants.CONTAINER_ELEMENT_TAG); for (int index = 0; index < planningViewConfigurationColumnNamesContainer.getLength(); ++index) { Node container = planningViewConfigurationColumnNamesContainer.item(index); Element codeWhoTotalNode = document.createElement(PREFIX + CODE_ELEMENT_NAME); codeWhoTotalNode.setTextContent(MigrationTo20.LEGACY_READABLE_ASSIGNED_WHO_TOTAL_CODE); container.appendChild(codeWhoTotalNode); }// w ww .ja v a 2 s . c o m verifyMigratedXmpz2(document); }
From source file:org.miradi.xml.TestXmpz2ForwardMigration.java
private void appendChildNodeWithSampleText(Document document, Node node, final String childElementName, final String value) { Element childNode = document.createElement(PREFIX + childElementName); childNode.setTextContent(value); node.appendChild(childNode);//from w w w.j a v a 2 s . c om }
From source file:org.miradi.xml.TestXmpz2ForwardMigration.java
private void appendContainerWithSampleCode(Document document, Node node, final String containerName) { Element containerNode = document.createElement(PREFIX + containerName); Element codeNode = document.createElement(CODE_ELEMENT_NAME); codeNode.setTextContent("Some value"); containerNode.appendChild(codeNode); node.appendChild(containerNode);/*w w w .j ava 2s. co m*/ }
From source file:org.mrgeo.resources.tms.TileMapServiceResource.java
protected static Document mrsPyramidMetadataToTileMapXml(final String raster, final String url, final MrsPyramidMetadata mpm) throws ParserConfigurationException { /*//w ww. j a va 2s.c om * String tileMap = "<?xml version='1.0' encoding='UTF-8' ?>" + * "<TileMap version='1.0.0' tilemapservice='http://localhost/mrgeo-services/api/tms/1.0.0'>" + * " <Title>AfPk Elevation V2</Title>" + " <Abstract>A test of V2 MrsPyramid.</Abstract>" * + " <SRS>EPSG:4326</SRS>" + " <BoundingBox minx='68' miny='33' maxx='72' maxy='35' />" + * " <Origin x='68' y='33' />" + * " <TileFormat width='512' height='512' mime-type='image/tiff' extension='tif' />" + * " <TileSets profile='global-geodetic'>" + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/1' units-per-pixel='0.3515625' order='1' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/2' units-per-pixel='0.17578125' order='2' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/3' units-per-pixel='0.08789063' order='3' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/4' units-per-pixel='0.08789063' order='4' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/5' units-per-pixel='0.08789063' order='5' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/6' units-per-pixel='0.08789063' order='6' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/7' units-per-pixel='0.08789063' order='7' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/8' units-per-pixel='0.08789063' order='8' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/9' units-per-pixel='0.08789063' order='9' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/10' units-per-pixel='0.08789063' order='10' />" * + " </TileSets>" + "</TileMap>"; */ final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); final DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements final Document doc = docBuilder.newDocument(); final Element rootElement = doc.createElement("TileMap"); doc.appendChild(rootElement); final Attr v = doc.createAttribute("version"); v.setValue(VERSION); rootElement.setAttributeNode(v); final Attr tilemapservice = doc.createAttribute("tilemapservice"); tilemapservice.setValue(normalizeUrl(normalizeUrl(url).replace(raster, ""))); rootElement.setAttributeNode(tilemapservice); // child elements final Element title = doc.createElement("Title"); title.setTextContent(raster); rootElement.appendChild(title); final Element abst = doc.createElement("Abstract"); abst.setTextContent(""); rootElement.appendChild(abst); final Element srs = doc.createElement("SRS"); srs.setTextContent(SRS); rootElement.appendChild(srs); final Element bbox = doc.createElement("BoundingBox"); rootElement.appendChild(bbox); final Attr minx = doc.createAttribute("minx"); minx.setValue(String.valueOf(mpm.getBounds().w)); bbox.setAttributeNode(minx); final Attr miny = doc.createAttribute("miny"); miny.setValue(String.valueOf(mpm.getBounds().s)); bbox.setAttributeNode(miny); final Attr maxx = doc.createAttribute("maxx"); maxx.setValue(String.valueOf(mpm.getBounds().e)); bbox.setAttributeNode(maxx); final Attr maxy = doc.createAttribute("maxy"); maxy.setValue(String.valueOf(mpm.getBounds().n)); bbox.setAttributeNode(maxy); final Element origin = doc.createElement("Origin"); rootElement.appendChild(origin); final Attr x = doc.createAttribute("x"); x.setValue(String.valueOf(mpm.getBounds().w)); origin.setAttributeNode(x); final Attr y = doc.createAttribute("y"); y.setValue(String.valueOf(mpm.getBounds().s)); origin.setAttributeNode(y); final Element tileformat = doc.createElement("TileFormat"); rootElement.appendChild(tileformat); final Attr w = doc.createAttribute("width"); w.setValue(String.valueOf(mpm.getTilesize())); tileformat.setAttributeNode(w); final Attr h = doc.createAttribute("height"); h.setValue(String.valueOf(mpm.getTilesize())); tileformat.setAttributeNode(h); final Attr mt = doc.createAttribute("mime-type"); mt.setValue("image/tiff"); tileformat.setAttributeNode(mt); final Attr ext = doc.createAttribute("extension"); ext.setValue("tif"); tileformat.setAttributeNode(ext); final Element tilesets = doc.createElement("TileSets"); rootElement.appendChild(tilesets); final Attr profile = doc.createAttribute("profile"); profile.setValue("global-geodetic"); tilesets.setAttributeNode(profile); for (int i = 0; i <= mpm.getMaxZoomLevel(); i++) { final Element tileset = doc.createElement("TileSet"); tilesets.appendChild(tileset); final Attr href = doc.createAttribute("href"); href.setValue(normalizeUrl(normalizeUrl(url)) + "/" + i); tileset.setAttributeNode(href); final Attr upp = doc.createAttribute("units-per-pixel"); upp.setValue(String.valueOf(180d / 256d / Math.pow(2, i))); tileset.setAttributeNode(upp); final Attr order = doc.createAttribute("order"); order.setValue(String.valueOf(i)); tileset.setAttributeNode(order); } return doc; }