Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6970123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:40:11+00:00 2026-05-27T16:40:11+00:00

I have the following code for converting the elements of an XML file into

  • 0

I have the following code for converting the elements of an XML file into a String using Stax:

private static XMLStreamReader getReader(InputStream inputStream) throws XMLStreamException {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    xmlInputFactory.setProperty("javax.xml.stream.isValidating", false);
    xmlInputFactory.setProperty("javax.xml.stream.supportDTD", false);
    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(inputStream);
    return xmlStreamReader;
}

private static String readElement(XMLStreamReader reader) throws XMLStreamException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    StAXSource source = new StAXSource(reader);
    t.transform(source, new StreamResult(outputStream));
    return outputStream.toString();
}

public static void main(String[] args) throws Exception {
    InputStream inputStream = new FileInputStream("c:\\temp\\test.xml");

    XMLStreamReader xmlStreamReader = getReader(inputStream);
    int count = 0;
    while (xmlStreamReader.hasNext()) {
        int eventType = xmlStreamReader.next();
        if (eventType == XMLEvent.START_ELEMENT) {
            String elementName = xmlStreamReader.getName().getLocalPart();
            if (!elementName.toLowerCase().equals("element")) {
                continue;
            }
            String productStr = readElement(xmlStreamReader);
            System.out.println(productStr);
        }
    }
}

}

This works fine on the following XML fragment:

<testDoc>
  <element>
    <a>hello world</a>
    <b>hello world again</b>
  </element>
  <element>
    <a>foo</a>
    <b>foo bar</b>
  </element>
</testDoc>

However, there are problems with this fragment where the </element> and <element> are on the same line:

<testDoc>
  <element>
    <a>hello world</a>
    <b>hello world again</b>
  </element><element>
    <a>foo</a>
    <b>foo bar</b>
  </element>
</testDoc>

In the second example it only seems to process the first element and not the second one. Any ideas?

Update:

I got it to work with the following code:

public static void main(String[] args) throws Exception {
    InputStream inputStream = new FileInputStream("c:\\temp\\test.xml");

    XMLStreamReader xmlStreamReader = getReader(inputStream);
    int count = 0;
    while (xmlStreamReader.hasNext()) {
        int eventType = xmlStreamReader.getEventType();
        if (eventType == XMLEvent.START_ELEMENT) {
            String elementName = xmlStreamReader.getName().getLocalPart();
            if (!elementName.toLowerCase().equals("element")) {
                xmlStreamReader.next();
                continue;
            }

            System.out.println(readElement(xmlStreamReader));
        } else {
            xmlStreamReader.next();
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:40:12+00:00Added an answer on May 27, 2026 at 4:40 pm

    Looks like a bug to me. You don’t say which Stax parser you are using: some of them are pretty ropey. Woodstox is the most reliable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code for converting the integer(a score) into the character and
I have following code snippet into c# public class Client { public string ID
I am converting Java into C# and have the following code (see discussion in
I am converting Bitmap into Drawable using following code. MemoryCache memoryCache = new MemoryCache();
i have the following code that queries a db and populates a datatable. private
I am using the following code for parsing of small xml files and it
I have the following java code: nameFinderModel = NameFinderME.train(en, organization, sampleStream, Collections.<String, Object>emptyMap()); And
I am manually converting Java to C# and have the following code: for (Iterator<SGroup>
I have the following code: std::string F() { WideString ws = GetMyWideString(); std::string ret;
I have XML data as a string which has to parsed, I am converting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.