cancel
Showing results for 
Search instead for 
Did you mean: 

Parse xml form

Former Member
0 Kudos

Hi,

I need to parse a KM folder that contains xml files created by XML Forms Builder. I need to get to a specific node in the xml. For example if i have the nodes: "Subject "Date" "Title" ... i need to show only the "Subject" node.

I succeed in getting the InputStream by using the code -

resource.getUnfilteredContent().getInputStream()

But i don't know how to proceed.

Thanks in advance,

Keren

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Prakash,

Here is the xml code:

<?xml version="1.0" encoding="UTF-8" ?>

- <!--

Description Resource XML File for Form Project NewsMsg

Copyright Copyright (c) SAP AG 2005

Author XML Forms Document Builder 1.0

Version 3.0 2005-07-31

-->

- <ROOT CONV_VERSION="1.1">

- <RESOURCES>

<TEXT AKEY="but_1122369051205">Save</TEXT>

<TEXT AKEY="lbl_1122369051229">Name</TEXT>

<TEXT AKEY="msg_1122369051231">Date</TEXT>

<TEXT AKEY="lbl_1122369051237">Subject</TEXT>

<TEXT AKEY="msg_1122369051239">Body</TEXT>

<TEXT AKEY="but_1122369051244">Reset</TEXT>

<TEXT AKEY="but_1122369051245">Cancel</TEXT>

<TEXT AKEY="but_1122369051247" SKIPTRANSLATION="TRUE">...</TEXT>

<TEXT AKEY="tit_global_displayName">NewsMsg</TEXT>

</RESOURCES>

</ROOT>

The java source:

IUser user = WPUMFactory.getUserFactory().getEP5User(request.getUser());

IResourceContext ctxt = new ResourceContext(user);

RID rid = RID.getRID("/documents/MyNews");

IResource resource = ResourceFactory.getInstance().getResource(rid, ctxt);

ICollection collection = (ICollection)resource;

IResourceList list = collection.getChildren();

IResource tmpRes = null;

InputStream input = null;

for(int i = 0; i < list.size(); i++)

{

tmpRes = list.get(i);

input = tmpRes.getUnfilteredContent().getInputStream();

}

Thanks,

Keren

Former Member
0 Kudos

Hi Keren,

This is what you do.

1. Add the following to your main component.

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
xmlContentHandler handler = new xmlContentHandler();
reader.setContentHandler(handler);
reader.parse(new InputSource(input));
Iterator it = handler.textList.iterator();
while (it.hasNext()) {
 String text = (String) it.next();
 response.write(text + "<br>");
}

2. Your import in your main component for SAX should be following.

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

3. Create a new class called <b>xmlContentHandler</b> like following.

 
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

public class xmlContentHandler implements ContentHandler
{
    public List textList = new ArrayList();
    StringBuffer buffer = new StringBuffer(); 
    /* (non-Javadoc)
     * @see org.xml.sax.ContentHandler#characters(char[], int, int)
     */
    public void characters(char[] ch, int start, int length) throws SAXException
    {
        buffer.append(ch, start, length);
    }

    /* (non-Javadoc)
     * @see org.xml.sax.ContentHandler#endDocument()
     */
    public void endDocument() throws SAXException
    {
        // TODO Auto-generated method stub

    }
    /* (non-Javadoc)
         * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
         */
        public void endElement(String namespaceURI, String localName, String qName) throws SAXException
        {
            if("TEXT".equalsIgnoreCase(qName))
            {
                textList.add(buffer.toString().trim());
				this.buffer = new StringBuffer();
            }
          
        }

        /* (non-Javadoc)
         * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
         */
        public void endPrefixMapping(String prefix) throws SAXException
        {
            // TODO Auto-generated method stub

        }

        /* (non-Javadoc)
         * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
         */
        public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
        {
            // TODO Auto-generated method stub

        }

        /* (non-Javadoc)
         * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
         */
        public void processingInstruction(String target, String data) throws SAXException
        {
            // TODO Auto-generated method stub

        }

        /* (non-Javadoc)
         * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
         */
        public void setDocumentLocator(Locator locator)
        {
            // TODO Auto-generated method stub

        }

        /* (non-Javadoc)
         * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
         */
        public void skippedEntity(String name) throws SAXException
        {
            // TODO Auto-generated method stub

        }

        /* (non-Javadoc)
         * @see org.xml.sax.ContentHandler#startDocument()
         */
        public void startDocument() throws SAXException
        {
        
        }

        /* (non-Javadoc)
         * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
         */
        public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException
        {
        

        }

        /* (non-Javadoc)
         * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
         */
        public void startPrefixMapping(String prefix, String uri) throws SAXException
        {
            // TODO Auto-generated method stub

        }


    }

PS: Please don't forget to reward points if the problem is resolved or the answer is helpful.

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Prakash,

Thanks a lot, it works!!!

But i had to remove the line:

if("TEXT".equalsIgnoreCase(qName))

in the endElement method.

Regards,

Keren

Former Member
0 Kudos

Hi Keren,

You have to use SAXParser to get individual node. If post your XML content then i could send you the code to parse it.

Subrato: He is asking for the java based solution.

regards,

Prakash

Former Member
0 Kudos

Hi

When u have created xml forms it will be creating XSL of corresponding forms in KMcontent repository called /etc/xmlforms/projectName folder .please edit those xml files online using edit online and hide the properties by appropriate use of xsl tags .then save it and and if possible restart the server. u can also change the xsl files by custom develop xsl files and use KMupload Iview to upload tose xsl files to target folder

/etc/xmlforms/projectName by changing path property in the iview. hope this helps

<b>With Regards

Subrato kundu

IBM

SAP Enterprise Portal TechnologyConsultant</b>