private Node findApplicationNode(Document manifestDocument) { NodeList applicationNodes = manifestDocument.getElementsByTagName("application"); if (applicationNodes.getLength() > 1) { throw new RuntimeException("found " + applicationNodes.getLength() + " application elements"); } return applicationNodes.item(0); }
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, TransformerException { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(new File("document.xml")); NodeList nodeList = document.getElementsByTagName("*"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { // do something with the current element System.out.println(node.getNodeName()); } } }
/** * Get value of given tag in 'svn info' document. * @param document document object containing {@code info} contents * @param tagName name of the tag to return value for * @return value string */ private String getInfoPart(Document document, String tagName) { return getValue(document.getElementsByTagName(tagName).item(0)); }
DocumentBuilderFactory factory = getFactory(false, false); try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new IOException("Cannot create parser satisfying configuration parameters: " + e, e); //NOI18N for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i) instanceof DocumentType) { doctype = (DocumentType) nl.item(i); doc = builder.newDocument(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (!(node instanceof DocumentType)) { try { nl = doc.getElementsByTagName("*"); // NOI18N for (int i = 0; i < nl.getLength(); i++) { Element e = (Element) nl.item(i); removeXmlBase(e);
public void testEncode() throws Exception { Ows10Factory f = Ows10Factory.eINSTANCE; GetCapabilitiesType caps = f.createGetCapabilitiesType(); AcceptVersionsType versions = f.createAcceptVersionsType(); caps.setAcceptVersions(versions); versions.getVersion().add("1.0.0"); versions.getVersion().add("1.1.0"); ByteArrayOutputStream output = new ByteArrayOutputStream(); response.write(caps, output, null); Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); TransformerFactory.newInstance() .newTransformer() .transform( new StreamSource(new ByteArrayInputStream(output.toByteArray())), new DOMResult(d)); assertEquals("ows:GetCapabilities", d.getDocumentElement().getNodeName()); assertEquals(2, d.getElementsByTagName("ows:Version").getLength()); } }
@Test public void removeInvalidGatewayReceiversWithDifferentBindAddresses() throws Exception { Document document = XmlUtils.createDocumentFromXml(getDuplicateReceiversWithDifferentBindAddressesXml()); System.out.println("Initial document:\n" + XmlUtils.prettyXml(document)); assertThat(document.getElementsByTagName("gateway-receiver").getLength()).isEqualTo(2); service.removeInvalidGatewayReceivers(document); System.out.println("Processed document:\n" + XmlUtils.prettyXml(document)); assertThat(document.getElementsByTagName("gateway-receiver").getLength()).isEqualTo(0); }
import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class Demo { public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(new File("input.xml")); NodeList nodeList = document.getElementsByTagName("Item"); for(int x=0,size= nodeList.getLength(); x<size; x++) { System.out.println(nodeList.item(x).getAttributes().getNamedItem("name").getNodeValue()); } } }
private static String getTagAttributeText(final Document doc, final String tag, final String attribute) { NodeList elementsByTagName = doc.getElementsByTagName(tag); for (int i = 0; i < elementsByTagName.getLength(); ++i) { Node item = elementsByTagName.item(i); Node namedItem = item.getAttributes().getNamedItem(attribute); if (namedItem != null) { return namedItem.getTextContent(); } } return null; }
@Test public void keepValidGatewayReceiversWithDefaultBindAddress() throws Exception { Document document = XmlUtils.createDocumentFromXml(getSingleReceiverWithDefaultBindAddressXml()); System.out.println("Initial document:\n" + XmlUtils.prettyXml(document)); assertThat(document.getElementsByTagName("gateway-receiver").getLength()).isEqualTo(1); service.removeInvalidGatewayReceivers(document); System.out.println("Processed document:\n" + XmlUtils.prettyXml(document)); assertThat(document.getElementsByTagName("gateway-receiver").getLength()).isEqualTo(1); }
protected void processRecordFilter(Document doc) { NodeList nodeList = doc.getElementsByTagName(NDCTEXTFILTER); // there is only one value stored Node n = nodeList.item(0); // add check for backwards compatibility as this feature was added in // version 1.2 if (n == null) { return; } NamedNodeMap map = n.getAttributes(); String text = getValue(map, NAME); if (text == null || text.equals("")) { return; } _monitor.setNDCLogRecordFilter(text); }
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource src = new InputSource(); src.setCharacterStream(new StringReader(xml)); Document doc = builder.parse(src); String age = doc.getElementsByTagName("age").item(0).getTextContent(); String name = doc.getElementsByTagName("name").item(0).getTextContent();
private void parseUsedPermissions(Document manifestDocument) { NodeList elementsByTagName = manifestDocument.getElementsByTagName("uses-permission"); int length = elementsByTagName.getLength(); for (int i = 0; i < length; i++) { Node node = elementsByTagName.item(i).getAttributes().getNamedItem("android:name"); usedPermissions.add(node.getNodeValue()); } }
@Test public void removeInvalidGatewayReceiversWithDifferentHostNameForSenders() throws Exception { Document document = XmlUtils.createDocumentFromXml(getDuplicateReceiversWithDifferentHostNameForSendersXml()); System.out.println("Initial document:\n" + XmlUtils.prettyXml(document)); assertThat(document.getElementsByTagName("gateway-receiver").getLength()).isEqualTo(2); service.removeInvalidGatewayReceivers(document); System.out.println("Processed document:\n" + XmlUtils.prettyXml(document)); assertThat(document.getElementsByTagName("gateway-receiver").getLength()).isEqualTo(0); }
/** * Removes "debug" tag from file * * @param file AndroidManifest file * @throws AndrolibException */ public static void removeApplicationDebugTag(File file) throws AndrolibException { if (file.exists()) { try { Document doc = loadDocument(file); Node application = doc.getElementsByTagName("application").item(0); // load attr NamedNodeMap attr = application.getAttributes(); Node debugAttr = attr.getNamedItem("android:debuggable"); // remove application:debuggable if (debugAttr != null) { attr.removeNamedItem("android:debuggable"); } saveDocument(file, doc); } catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) { } } }
private static Source mergeIntoSource(InputStream baseXmlStream, InputStream xmlStream) throws Exception { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setIgnoringComments(true); domFactory.setValidating(false); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document baseDoc = builder.parse(baseXmlStream); Document ruleDoc = builder.parse(xmlStream); // Shall this be more generic, i.e. reuse not just unification ??? NodeList unificationNodes = baseDoc.getElementsByTagName("unification"); Node ruleNode = ruleDoc.getElementsByTagName("rules").item(0); Node firstChildRuleNode = ruleNode.getChildNodes().item(1); for (int i = 0; i < unificationNodes.getLength(); i++) { Node unificationNode = ruleDoc.importNode(unificationNodes.item(i), true); ruleNode.insertBefore(unificationNode, firstChildRuleNode); } return new DOMSource(ruleDoc); }
public static void collectPatterns(Document document) { NodeList nl = document.getElementsByTagName("pattern"); while(nl.getLength() > 0) { Element pt = (Element) nl.item(0); String id = pt.getAttribute("id"); patterns.put(id, pt); pt.getParentNode().removeChild(pt); } }
@Test public void removeDuplicateGatewayReceiversWithDefaultProperties() throws Exception { Document document = XmlUtils.createDocumentFromXml(getDuplicateReceiversWithDefaultPropertiesXml()); System.out.println("Initial document:\n" + XmlUtils.prettyXml(document)); assertThat(document.getElementsByTagName("gateway-receiver").getLength()).isEqualTo(2); service.removeDuplicateGatewayReceivers(document); System.out.println("Processed document:\n" + XmlUtils.prettyXml(document)); assertThat(document.getElementsByTagName("gateway-receiver").getLength()).isEqualTo(1); }
protected static List<Triple<Short,String,Boolean>> parse(InputStream stream) throws Exception { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); factory.setValidating(false); // for now DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse(stream); NodeList class_list=document.getElementsByTagName("class"); List<Triple<Short,String,Boolean>> list=new LinkedList<>(); for(int i=0; i < class_list.getLength(); i++) { if(class_list.item(i).getNodeType() == Node.ELEMENT_NODE) { list.add(parseClassData(class_list.item(i))); } } return list; }
public String getSample( String strFunctionName, String strFunctionNameWithArgs ) { String sRC = "// Sorry, no Script available for " + strFunctionNameWithArgs; NodeList nl = dom.getElementsByTagName( "jsFunction" ); for ( int i = 0; i < nl.getLength(); i++ ) { if ( nl.item( i ).getAttributes().getNamedItem( "name" ).getNodeValue().equals( strFunctionName ) ) { Node elSample = ( (Element) nl.item( i ) ).getElementsByTagName( "sample" ).item( 0 ); if ( elSample.hasChildNodes() ) { return ( elSample.getFirstChild().getNodeValue() ); } } } return sRC; }
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setIgnoringComments(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(new File("XmlTest.xml")); NodeList nodes = doc.getElementsByTagName("CustomerId"); Text a = doc.createTextNode("value"); Element p = doc.createElement("newNode"); p.appendChild(a); nodes.item(0).getParentNode().insertBefore(p, nodes.item(0));