java - DOM avoid preserve whitespaces -
is way avoid preserve whitespaces in dom (whichever java library) ?
i have xml file validated xsd schema. schema, <text>
element contains texts. element contains element nodes. when edit xml file, visibility, have several types of whitespaces tab, blank, carriage return,...
how can parse xml (without xslt, java libraries) without preserve whitespaces not authorized schema ?
https://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/documentbuilderfactory.html#setignoringelementcontentwhitespace(boolean) suggests there setting "requires parser in validating mode" (https://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/documentbuilderfactory.html#setschema(javax.xml.validation.schema)) , supports ignoring white space in element content models.
here example, given java code
documentbuilderfactory dbf = documentbuilderfactory.newinstance(); dbf.setnamespaceaware(true); dbf.setignoringelementcontentwhitespace(true); schema schema = schemafactory.newinstance(xmlconstants.w3c_xml_schema_ns_uri).newschema(new file("schema1.xsd")); //dbf.setschema(schema); documentbuilder db = dbf.newdocumentbuilder(); document doc = db.parse("file1.xml"); system.out.println(doc.getdocumentelement().getchildnodes().getlength());
with sample file
<root> <item>a</item> <item>b</item> </root>
the number of child nodes output 5, when remove comment from
dbf.setschema(schema);
and have schema defining element contents root
element e.g.
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/xmlschema" elementformdefault="qualified"> <xs:element name="root"> <xs:complextype> <xs:sequence maxoccurs="unbounded"> <xs:element name="item" type="xs:string"/> </xs:sequence> </xs:complextype> </xs:element> </xs:schema>
the output child nodes 2.
Comments
Post a Comment