Results 1 to 3 of 3
  1. #1
    Tharun Jay is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Exclamation Need XML splitting

    I need to split an xml in to multiple as below in java:
    Input xml:
    <Product>
    <Category Name="Node">
    <Item ID="aaa" Desc="sample item"/>
    <Item ID="bbb" Desc="sample item"/>
    <Item ID="ccc" Desc="sample item"/>
    <Item ID="ddd" Desc="sample item"/>
    </Category>
    </Product>

    My Ouput xml should be:
    <Header>
    <Product>
    <Category Name="Node">
    <Item ID="aaa" Desc="sample item"/>
    <Item ID="bbb" Desc="sample item"/>
    </Category>
    </Product>
    <Product>
    <Category Name="Node">
    <Item ID="ccc" Desc="sample item"/>
    <Item ID="ddd" Desc="sample item"/>
    </Category>
    </Product>
    </Header>

    Thanks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Need XML splitting

    To learn how to parse XML files you should check out Trail: Java API for XML Processing (JAXP) (The Java™ Tutorials). I have used the SAXParser to do some of this with little difficulty.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Tharun Jay is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Re: Need XML splitting

    Hey Jim,
    Thanks for you reply.
    I have tried the below code am able to get below output. But i need to get "Category" also.

    I need to make this dynamic, that is possible but to make that happen i need to get the category also in this so whatever path i give it should split.

    an output as:

    <Header>
    <Product>
    <Item Desc="sample item" ID="aaa" />
    <Item Desc="sample item" ID="bbb" />
    </Product>
    <Product>
    <Item Desc="sample item" ID="ccc" />
    <Item Desc="sample item" ID="ddd" />
    </Product>
    </Header>



    XPath xPath = XPathFactory.newInstance().newXPath();

    FileReader reader = new FileReader("D:\\input1.xml");
    InputSource xml = new InputSource(reader);

    int iNoofItems = 2;
    NodeList titleNodes = (NodeList) xPath.evaluate("//Product/Category/Item", xml, XPathConstants.NODESET);
    StringWriter sw = null;
    String sOutXML = "<Header><Product>";
    int iCount=1;

    for(int x=0; x<titleNodes.getLength(); x++) {


    sw = new StringWriter();

    Node nCharge= titleNodes.item(x);
    //Element eOrderLineDetails = (Element)nCharge;
    //String sXML = new XMLDocument(element).toString();
    try {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATIO N, "yes");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.transform(new DOMSource(nCharge), new StreamResult(sw));
    } catch (TransformerException te) {
    System.out.println("nodeToString Transformer Exception");
    }
    //return sw.toString();
    //System.out.println(sw.toString());
    sOutXML = sOutXML+sw.toString();

    if(iCount==titleNodes.getLength())
    sOutXML = sOutXML + "</Product>";
    else if(iCount%iNoofItems == 0)
    {
    sOutXML = sOutXML + "</Product><Product>";
    }
    iCount++;

    }

    sOutXML = sOutXML + "</Header>";
    System.out.println("OUTPUT : "+sOutXML);
    }

Similar Threads

  1. Image splitting twice
    By tzeus in forum New To Java
    Replies: 4
    Last Post: 12-10-2013, 01:55 AM
  2. splitting paragraph
    By kcananda in forum New To Java
    Replies: 1
    Last Post: 08-11-2012, 09:31 PM
  3. Splitting the string[]
    By issoa in forum New To Java
    Replies: 1
    Last Post: 02-28-2012, 07:56 AM
  4. Splitting
    By marksey07 in forum New To Java
    Replies: 2
    Last Post: 01-05-2011, 02:55 AM
  5. Array splitting
    By Lunarion in forum New To Java
    Replies: 3
    Last Post: 04-17-2009, 09:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •