473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using xerces to parse a string of xml does not seem to work

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
Jul 20 '05 #1
2 6279


Watsh wrote:
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());


toString is not reliable to find out whether the document has been
parsed as I think the result of toString depends on the
implementation/parser used, it can return anything the parser
implementor wants it to return.
Check
System.out.println(document.getElementsByTagName(" *"))
and I am sure it shows that there are elements in the document.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #2
"Watsh" <rw****@hp.com> wrote in message
news:a5**************************@posting.google.c om...
String xml = "<a><b/></a>";


What happens if you prepend an XML declaration?

/kmc
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: bugbear | last post by:
Subject pretty much says it all. I'd like to parse XML (duh!) using Xerces (because its fast, and reliable, and comprehensive, and supports lots of features). I'd like to conform to standards...
1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
4
by: joes | last post by:
Hello there I tried for several days to get a simple validation with xml schema & xerces working. Goal for me is tuse JAXP and not specific Xerces classes. I don't get the point what I am doing...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
3
by: Girish | last post by:
Hi All, I have written a component(ATL COM) that wraps Xerces C++ parser. I am firing necessary events for each of the notifications that I have handled for the Content and Error handler. The...
0
by: atlantis | last post by:
Hi, I have a very strange problem with xsl:import when usig RELATIVE path on AIX 5.2 server. I have two XSL files in the same directory: "ists_xslt3.xsl" and "ists_xslt3_layout.xsl". This...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
2
by: muralibala68 | last post by:
Hi, How do I parse an xml message that is in std::string using sax2 parse method that does not seem to take a string and expects a filename? Thanks.
1
by: Sidhartha | last post by:
Hi, I am facing a problem while parsing local language characters using sax parser. We use DOM to parse and SAX to read the source. But when our application parses strings with local language...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.