W
Watsh
Hi All,
I have been trying to parse an XML string using the StringReader and
InputSource interface but the document returned to me is always null.
Please find the code below which i have been experimenting with:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import java.io.StringReader;
public class Test {
/** Creates a new instance of Test */
public Test()
{
}
public static void main(String[] args)
{
try {
String xml = "<a><b/></a>";
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
// set namespace and validating options on factory, if
necessary
factory.setValidating(false);
factory.setIgnoringComments(true);
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader reader = new StringReader(xml);
InputSource source = new InputSource(reader);
Document document = builder.parse(source);
System.out.println(document.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Any pointers on how to resolve this issue will be of great help also
any tip for an alternative way is just as well fine for me.
Warm Regards,
Watsh
I have been trying to parse an XML string using the StringReader and
InputSource interface but the document returned to me is always null.
Please find the code below which i have been experimenting with:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import java.io.StringReader;
public class Test {
/** Creates a new instance of Test */
public Test()
{
}
public static void main(String[] args)
{
try {
String xml = "<a><b/></a>";
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
// set namespace and validating options on factory, if
necessary
factory.setValidating(false);
factory.setIgnoringComments(true);
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader reader = new StringReader(xml);
InputSource source = new InputSource(reader);
Document document = builder.parse(source);
System.out.println(document.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Any pointers on how to resolve this issue will be of great help also
any tip for an alternative way is just as well fine for me.
Warm Regards,
Watsh