import nu.xom.*; Builder = new Builder(); Document doc = builder.build(new java.io.StringBufferInputStream(inputXml)); Nodes nodes = doc.query("person"); Element homePhone = new Element("home"); homePhone.addChild(new Text("555-555-5555")); Element workPhone = new Element("work"); workPhone.addChild(new Text("555-555-5555")); Element phoneNumbers = new Element("phoneNumbers"); phoneNumbers.addChild(homePhone); phoneNumbers.addChild(workPhone); nodes[0].addChild(phoneNumbers); System.out.println(doc.toXML()); // should print modified xml
HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet("http://streak.espn.go.com/en/?date=20120824"); HttpResponse response = client.execute(request); // Check if server response is valid StatusLine status = response.getStatusLine(); if (status.getStatusCode() != 200) { throw new IOException("Invalid response from server: " + status.toString()); } // Pull content stream from response HttpEntity entity = response.getEntity(); InputStream inputStream = entity.getContent(); try { XMLReader parser = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser"); // Use the TagSoup parser to build an XOM document from HTML Document doc = new Builder(parser).build(builder.toString()); // Parse the document as needed Node node = doc.query("..."); } catch(IOException e) { ... }
String myxml = "<resp><status>good</status><msg>hi</msg></resp>"; Document document = new Builder().build(myxml, "test.xml"); Nodes nodes = document.query("/resp/status"); System.out.println(nodes.get(0).getValue());
Builder parse = new Builder(); Document xml = parse.build("/var/www/JAVA/toForum.xml"); System.out.println(xml.query("//location[@id=83]/*").get(0).getParent().toXML());
XMLReader tagsoup = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser"); Builder bob = new Builder(tagsoup); Document html = bob.build("http://www.yahoo.com"); Nodes images = html.query("//img"); for (int index = 0; index < images.size(); index++) { Element image = (Element) images.get(index); String src = image.getAttribute("src").getValue(); // do something with it... }
protected static Iterator<Element> getElementsFromXPath(final String xPath, final Document document) { Nodes regNodes = document.query(xPath); List<Element> elements = new ArrayList<>(); for (int i = 0; i < regNodes.size(); i++) { try { elements.add((Element) regNodes.get(i)); } catch (ClassCastException ex) { ExceptionPrinter.printHistory(new CouldNotPerformException("Invalide data from XPath[" + xPath + "]!", ex), LOGGER); } } return elements.iterator(); }
@Override public boolean apply(final BroadcastMessage broadcastMessage) { Document doc = broadcastMessage.signedBroadcastMessage.parsedBroadcastMessage.broadcastMessageDocument; for (UserIdentity identity : identityStore.getIdentitiesWithLabel(TrConstants.OWN)){ for(int docCount = 0; docCount<doc.query("//"+TrConstants.FormatInfo.MENTION).size(); docCount++){ if(identity.getNick().equals(doc.query("//"+TrConstants.FormatInfo.MENTION).get(docCount).getValue())){ return true; } } } return false; } }
public static Node getNode(Document doc, String entityType, String entityName) { String query = createQueryStringByName(entityType, entityName); Nodes nodes = doc.query(query); if (nodes != null && nodes.size() > 0) { return nodes.get(0); } return null; }
protected Element getElementFromXPath(final String xPath, final Document document) throws VerificationFailedException { Nodes regNodes = document.query(xPath); if (regNodes.size() != 1) { throw new VerificationFailedException("Invalide data from XPath[" + xPath + "]!"); } return (Element) regNodes.get(0); }
void bumpMajorVersion(Document pom) throws MojoExecutionException { XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes project = pom.query("/mvn:project", context); if (project.size() == 0) throw new MojoExecutionException("Pom is broken"); Nodes nodes = pom.query("/mvn:project/mvn:version", context); if (nodes.size() != 1) throw new MojoExecutionException("Version is not declared correctly"); Element e = (Element) nodes.get(0); String[] components = e.getValue().split("\\."); components[0] = Integer.toString(Integer.valueOf(components[0]) + 1); String bumpedVersion = String.join(".", components); Element newVersion = new Element("version", "http://maven.apache.org/POM/4.0.0"); newVersion.appendChild(bumpedVersion); ((Element) project.get(0)).replaceChild(e, newVersion); // TODO check that the next version does not already exist }
@Test public void bump() throws ValidityException, ParsingException, IOException, MojoExecutionException { Document pom = new Builder().build(new File(new File("src/it/update"), "pom.xml")); XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes versions = pom.query("/mvn:project/mvn:version", context); assertThat(versions.size()).isEqualTo(1); assertThat(pom.query("/mvn:project/mvn:version[text()='1.1-SNAPSHOT']", context).size()).isEqualTo(1); assertThat(pom.query("/mvn:project/mvn:version[text()='2.1-SNAPSHOT']", context).size()).isEqualTo(0); new StickyBoundsUpgradeMojo().bumpMajorVersion(pom); assertThat(pom.query("/mvn:project/mvn:version[text()='1.1-SNAPSHOT']", context).size()).isEqualTo(0); assertThat(pom.query("/mvn:project/mvn:version[text()='2.1-SNAPSHOT']", context).size()).isEqualTo(1); } @Test
@Override public ISource transform(ISource source) throws SourceException { Document d = source.getDocument(); Nodes tables = d.query("//table"); for (int i = 0; i < tables.size(); i++) { Element table = (Element) tables.get(i); table.addAttribute(new Attribute("class", getValue())); } return source; }
void updateProperty(Document pom, String propertyName, String newVersion) throws MojoExecutionException { XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes nodes = pom.query("//mvn:properties", context); if (nodes.size() > 0) { final Element propertiesElement = (Element) nodes.get(0); Elements properties = propertiesElement.getChildElements(); for (int i = 0; i < properties.size(); i++) { Element property = properties.get(i); if (property.getLocalName().equals(propertyName)) { Element newRange = new Element(propertyName, "http://maven.apache.org/POM/4.0.0"); newRange.appendChild(newVersion); propertiesElement.replaceChild(property, newRange); } } } }
void updateProperty(Document pom, String propertyName, String newVersion) throws MojoExecutionException { XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes nodes = pom.query("//mvn:properties", context); if (nodes.size() > 0) { final Element propertiesElement = (Element) nodes.get(0); Elements properties = propertiesElement.getChildElements(); for (int i = 0; i < properties.size(); i++) { Element property = properties.get(i); if (property.getLocalName().equals(propertyName)) { Element newRange = new Element(propertyName, "http://maven.apache.org/POM/4.0.0"); newRange.appendChild(newVersion); propertiesElement.replaceChild(property, newRange); } } } }
@Test public void updateWithClassifier() throws ValidityException, ParsingException, IOException, MojoExecutionException { Document pom = new Builder() .build(new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("classifiers.xml")))); Artifact artifact = new DefaultArtifact( "net.stickycode", "sticky-coercion", "jar", "", "[2.6,3)"); new StickyBoundsMojo().updateDependency(pom, artifact, "[2.1,3)"); XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes versions = pom.query("//mvn:version", context); assertThat(versions.size()).isEqualTo(4); Nodes nodes = pom.query("//mvn:version[text()='[2.6,3)']", context); assertThat(nodes.size()).isEqualTo(1); Node node = nodes.get(0); assertThat(node.getValue()).isEqualTo("[2.6,3)"); }
@Test public void update() throws ValidityException, ParsingException, IOException, MojoExecutionException { Document pom = new Builder().build(new File(new File("src/it/upgrade"), "pom.xml")); Artifact artifact = new DefaultArtifact( "net.stickycode", "sticky-coercion", "jar", "", "[3.6,4)"); new StickyBoundsUpgradeMojo().updateDependency(pom, artifact, "[3.1,4)"); XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes versions = pom.query("//mvn:version", context); assertThat(versions.size()).isEqualTo(3); Nodes nodes = pom.query("//mvn:version[text()='[3.6,4)']", context); assertThat(nodes.size()).isEqualTo(1); Node node = nodes.get(0); assertThat(node.getValue()).isEqualTo("[3.6,4)"); }
@Test public void update() throws ValidityException, ParsingException, IOException, MojoExecutionException { Document pom = new Builder().build(new File(new File("src/it/update"), "pom.xml")); Artifact artifact = new DefaultArtifact( "net.stickycode", "sticky-coercion", "jar", "", "[3.6,4)"); new StickyBoundsMojo().updateDependency(pom, artifact, "[3.1,4)"); XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0"); Nodes versions = pom.query("//mvn:version", context); assertThat(versions.size()).isEqualTo(3); Nodes nodes = pom.query("//mvn:version[text()='[3.6,4)']", context); assertThat(nodes.size()).isEqualTo(1); Node node = nodes.get(0); assertThat(node.getValue()).isEqualTo("[3.6,4)"); }
public String getOutputFragment(String inputFragment) { return new TestRig() .withFixture(this) .processFragment(inputFragment) .getXOMDocument() .query("//table").get(0) .toXML() .replaceAll("\u00A0", " "); }
public String process(String inputFragment) { return new TestRig() .withFixture(this) .processFragment(inputFragment) .getXOMDocument() .query("//table").get(0) .toXML() .replaceAll("\u00A0", " "); }}
public String markAsException(String fragment, String expression, String errorMessage) { Throwable t = new Throwable(errorMessage); t.setStackTrace(stackTraceElements.toArray(new StackTraceElement[0])); Element element = new Element((nu.xom.Element) new TestRig() .processFragment(fragment) .getXOMDocument() .query("//p") .get(0)); new ThrowableRenderer(new ClassPathSource()).throwableCaught(new ThrowableCaughtEvent(t, element, expression)); return element.toXML(); } }