Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Parsing XML

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Parsing XML

    I'm trying to parse an XML file into an XML file. So basically I'm appending some static elements, attributes, and Text Content for the most part, but I have about 16 dynamic TextContent fields that I want to parse from an existing XML document into my new one. The way I want to do it is to use a for statement the nodes to iterate through to pull the information. I'm getting an implementation error on the for statements and I'm not sure how else to do this without a for statement. Any ideas? Thanks. Here's the relevant code so far:

    public static void main(String[] args) {
     
            try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder loader = factory.newDocumentBuilder();
     
          Document input = loader.parse(new FileInputStream("c:\\XML\\input.xml"));
     
            Node Order = input.getDocumentElement();
     
            NodeList DetailsList = input.getElementsByTagName("Details");
     
            for (Node Detail : DetailsList) { 
     
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
     
            Document doc = docBuilder.newDocument();
     
            Element root = doc.createElement("MercuryGate");
            doc.appendChild(root);
     
    //...Various create element and append statements follow
     
    NodeList FieldsList = input.getElementsByTagName("Field");
     
            for (Node Field : FieldsList){
     
    //...My for statements that pull the content and then insert it into the setTextContent fields where necessary in my previous code
     
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();
     
        Source src = new DOMSource(doc);
        Result dest = new StreamResult("c:\\XML\\xmltest.xml");
        aTransformer.transform(src, dest);  
     
     
                }
        }
     
        catch(Exception e){
          System.out.println(e.getMessage());
        }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Parsing XML

    You need to provide the XML document you are attempting to parse and also show the desired output.
    We can take it from there..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Question regarding XML Parsing
    By newbie in forum Java Theory & Questions
    Replies: 2
    Last Post: February 24th, 2011, 06:03 AM
  2. Java XML Parsing
    By nimilc2002 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 9th, 2011, 11:00 AM
  3. Parsing urls
    By Riddhi Sharma in forum Java Theory & Questions
    Replies: 2
    Last Post: January 25th, 2011, 10:06 AM
  4. Parsing CDATA
    By Sai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 9th, 2010, 12:33 AM
  5. [SOLVED] Parsing ID3 tags from mp3
    By John in forum Java Theory & Questions
    Replies: 14
    Last Post: April 16th, 2009, 01:36 PM