Search in sources :

Example 1 with XMLDocument

use of com.jcabi.xml.XMLDocument in project wring by yegor256.

the class XePrint method text.

/**
 * Render text via XPath.
 * @param pattern Pattern to use
 * @return Plain text
 */
public String text(final CharSequence pattern) {
    final XML xml = new XMLDocument(new Xembler(this.dirs).domQuietly());
    final Pattern ptn = Pattern.compile("\\{([^}]+)}");
    final Matcher mtr = ptn.matcher(pattern);
    final StringBuffer out = new StringBuffer(pattern.length());
    while (mtr.find()) {
        mtr.appendReplacement(out, xml.xpath(mtr.group(1)).get(0).replace("\\", "\\\\").replace("$", "\\$"));
    }
    mtr.appendTail(out);
    return out.toString();
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) XML(com.jcabi.xml.XML) Xembler(org.xembly.Xembler) XMLDocument(com.jcabi.xml.XMLDocument)

Example 2 with XMLDocument

use of com.jcabi.xml.XMLDocument in project jcabi-github by jcabi.

the class JsonNodeTest method convertsXmlToJson.

/**
 * JsonNode can text XML to JSON.
 * @throws Exception If some problem inside
 */
@Test
public void convertsXmlToJson() throws Exception {
    final XML xml = new XMLDocument("<user><name>Jeff</name><dept><title>IT</title></dept></user>");
    final JsonObject json = new JsonNode(xml.nodes("user").get(0)).json();
    MatcherAssert.assertThat(json, Matchers.notNullValue());
    MatcherAssert.assertThat(json.getString("name"), Matchers.equalTo("Jeff"));
    MatcherAssert.assertThat(json.getJsonObject("dept").getString("title"), Matchers.equalTo("IT"));
}
Also used : XML(com.jcabi.xml.XML) JsonObject(javax.json.JsonObject) XMLDocument(com.jcabi.xml.XMLDocument) Test(org.junit.Test)

Aggregations

XML (com.jcabi.xml.XML)2 XMLDocument (com.jcabi.xml.XMLDocument)2 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 JsonObject (javax.json.JsonObject)1 Test (org.junit.Test)1 Xembler (org.xembly.Xembler)1