public Node parse(File f) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); FileInputStream in = new FileInputStream(f); int r; byte[] b = new byte[10240]; while ((r = in.read(b)) != -1) { bout.write(b, 0, r); } return parse(new Node(), new String(bout.toByteArray())); }
public Node parse(String s) { return parse(new Node(), s); }
@Test public void xml_comment_1() throws IOException { String s = "<div><!-- <comment> --></div>"; Node div = new XmlParser().parse(s); Assert.assertEquals(0, div.attributes().size()); Assert.assertEquals(1, div.children().size()); Assert.assertEquals(true, div.children().get(0).isComment()); Assert.assertEquals("<!--<comment>-->", div.children().get(0).ownText()); }
@Test public void xml_comment_2() throws IOException { String s = "" + "<div>\n" + " <!--[if IE]>\n" + " According to the conditional comment this is IE<br />\n" + " <![endif]-->\n" + "</div>\n"; Node div = new XmlParser().parse(s); Assert.assertEquals(0, div.attributes().size()); Assert.assertEquals(1, div.children().size()); Assert.assertEquals(true, div.children().get(0).isComment()); Assert.assertEquals("" + "<!--[if IE]>\n" + " According to the conditional comment this is IE<br />\n" + " <![endif]-->", div.children().get(0).ownText()); }
Assert.assertEquals("I say: '${hello}'", new XmlParser().parse(s).textOwn()); Assert.assertEquals("xml", new XmlParser().parse(s).name()); s = "<xml/>"; Assert.assertEquals("xml", new XmlParser().parse(s).name()); Assert.assertEquals(true, new XmlParser().parse(s).hasAttr("attr")); s = "<xml attr ><xml>"; Assert.assertEquals(true, new XmlParser().parse(s).hasAttr("attr")); s = "<xml attr />"; Assert.assertEquals(true, new XmlParser().parse(s).hasAttr("attr")); s = "<xml attr/>"; Assert.assertEquals(true, new XmlParser().parse(s).hasAttr("attr")); s = "<xml attr attr2/>"; Assert.assertEquals(true, new XmlParser().parse(s).hasAttr("attr")); Assert.assertEquals("qwerty", new XmlParser().parse(s).attr("attr")); s = "<xml attr=\"qwerty\" attr2/>"; Assert.assertEquals("qwerty", new XmlParser().parse(s).attr("attr")); s = "<xml attr2 attr=\"qwerty\"/>"; Assert.assertEquals("qwerty", new XmlParser().parse(s).attr("attr")); Assert.assertEquals(1, new XmlParser().parse(s).size()); s = "<xml><child/></xml>"; Assert.assertEquals(1, new XmlParser().parse(s).size()); s = "<xml><child attr=\"qwerty\"/></xml>"; Assert.assertEquals(1, new XmlParser().parse(s).size()); Assert.assertEquals("qwerty", new XmlParser().parse(s).first().attr("attr"));
" <!-- <comment> -->\n" + "</div>"; Node div = new XmlParser().parse(s); Assert.assertEquals(0, div.attributes().size()); Assert.assertEquals(3, div.children().size());