JDOM and Parsing

I have XML data in the form of a String as follows:
<root>
<section1>
... some data/tags
</section1>
<section2>
... some data/tags
</section2>
<section3>
... some data/tags
</section3>
</root>
What I would like to do is slightly tweak this XML to remove the whole of <section3>. I would like to use the latest JDOM-beta8 in order to do this. Once I remove this <section3> tag, then I am going to need the new XML String.
So, I guess I have a few questions:
1) How do I JDOM to read in a string to parse this data?
2) Could I use SAX or do I need to use DOM behind JDOM?
3) How do I remove this one node <section3> from the JDOM?
4) How do I get the String of this XML data back from JDOM?
Any help would be most appreciated. Thanks.
Tom

First, here is a snippet of what I do with JDOM to read/parse xml:
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
... some class definiton
private final SAXBuilder builder = new SAXBuilder(false);
private Document doc = null;
.. some method defintion
try
builder.setValidation(false); // seems to do nothing
doc = builder.build(new File("<path to xml file to read>"));
Element root = doc.getRootElement();
// see below for insertion that goes here
catch (JDOMException jdome)
jdome.printStackTrace(System.out);
Now, the above opens the xml file, gets you the root node. Now you are ready to do some parsing. In this case, you want to remove Section 3 from the XML and I assume spit the XML file back out? You say you need the new XML String, but not sure how you mean.
So, since Section 3 in your example is one node down, you need to do:
root.removeElement("Section3");
However, in the JDOM api it says that is one level deep, so I am not sure if it just removes the <Section3></Section3> tags, and not any children of that element. There is also
Element section3 = root.getChild("Element3");
if (null != section3)
root.removeContent(section3);
I have never done this before, but the removeContent says it removes the Element, so maybe it removes all children (nested elements) of this element? If not, you can just do something like:
Element section3 = root.getChild("Element3");
if (null != section3)
section3.removeChildren(); // removes all child elements
root.removeContent(section3);
One of those 3 should do the job, but to be honest I am not sure.
I think SAX or DOM would be fine. I think the thing with using SAX is, if you only have to go through the XML one time, it works fine and saves bit time on memory, is faster, etc. If you save the doc in memory for future use, you may need DOM to keep it in memory. Not for sure though. Watching this topic to see what others may say.
I am also not sure how to get the whole XML as a string, but there is a toString() method in the Element class. Since getRootElement() returns the root doc element, I assume toString() will print out the entire XML string. If you remove the Element first, then do a toString() on the root, you should get what you are looking for.

Similar Messages

  • Loading and parsing XML files

    I am currently working on an application in which I have a XML file that is 1.5 mb and has 1100 records that I need to load and parse. Loading takes less than a minute, but when I try to parse it with the DOM parser it's taking too much time. I am trying to put it into an array and then display it as if I'm tied to a database. I need a different approach. Does anyone have any experience and insight with the DOM parser? Would the SAX parser be a better way to go, why and how?

    You can use SAX... but SAX is good only if you want to read the data once.
    If you want to use the same data again and again then you might have to parse the file again... which prooves expensive.
    DOM will take too much of memory and CPU time.
    Have you tried using JDOM ? it is a new kind of a XML parsing utility that is quite lightweight and has good api to recurse the JDOM tree also.
    check it out at
    http://www.jdom.org.
    hope this helps.
    regards,
    Abhishek.

  • Loading xml and parsing

    Is there a way to load an xml file and parse the tags as we do in javascripts in java?
    i want to write a generic xml parser.
    what all should i know before starting.
    i choose java as my development language.

    Is there a way to load an xml file and parse the tags
    as we do in javascripts in java?Yes?
    >
    i want to write a generic xml parser.There is one built into Java or you can use JDOM or Dom4J.
    >
    what all should i know before starting.Difficult to say without knowing what you are doing but the Java tutorial is a good starting point.
    i choose java as my development language.

  • Verifying and parsing "query" in cm:select query="..." ...

    I'm writting a session EJB that's passed a param (String query) which is supposed
    to be any valid "query" string passed to the cm:select tag (see "http://edocs.bea.com/wlcs/docs31/p13ndev/jsptags.htm#1057716"
    for more info on this tag and the query param).
    My problem is this: I don't think I should write ALL the stuff to validate and
    parse this string because bea has already done it in: com.beasys.commerce.foundation.expression.*;
    but the only source of documentation available on those classes is the Javadoc(which
    isn't that complete). Has anyone used these classes before(Search, Expression,
    Criteria, Logical)? Does anyone know of some documents on how to use them?
    Please help if you can. I'd really appreciate it. Thanks.

    rajan please just google or search SDN. there are large number of post for this..
    to give you a head start: for using a particular index in the select query a %_HINTS ORACLE 'INDEX clause is added

  • Repeated retrieval and parsing of XML file causes IE to display an error message

    I have a flash application that makes a HTTP call every 120
    seconds to retrieve a xml file. This file is being generated with
    fresh data every few minutes or so and pushed to the apache web
    root with a unix mv command.
    I'm using the standard Flash XML object. The HTTP request is
    NOT over SSL (I know there is a bug related to this). Here is the
    code to make the retrieval:
    xmlData = null;
    xmlData = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = xmlTraverse;
    xmlData.load("
    http://domain/dir/somefile.xml");
    The "xmlTraverse" method was written by my team. There is no
    known bug in the parsing logic.
    Intermittently and unpredictably, IE will display the
    following dialogue box:
    "A script in this movie is causing Macromedia Flash Player 8
    to run slowly. If it continues to run, your computer may become
    unresponive. Do you want to abort the script?"
    I originally leaned toward an infinite loop causing this
    problem. The timeline jumps back and forth between two frames,
    checking in the latter frame if the xml has been loaded and parsed
    before allowing execution to move to the next frame. I hardcoded
    the loop check to always be false, but this did not produce the
    message above (although it pegged my CPU).
    The only scenario where I've managed to reproduce the message
    above is by making a huge (50 MB) xml file. This consistently
    produces the message. But I don't realistically see how our xml
    file could ever be over 1 MB.
    First, can anyone cite another cause for this message? I'm
    starting to lean towards the issue lying in our parsing logic such
    as infinite recursion on a badly formed XML file. Second, does
    anyone know of a solid xml SAX parsing actionscript utility where
    you can assign callbacks to XML nodes? If the problem is in the
    parsing, we may have to replace our homegrown solution with
    something more robust and proven.

    Is it a recursive parsing function? I'm not sure about this,
    but I think this message pops up when there are more than 256
    iterations in a loop (I've read something about this 256 limit,
    that will end while loops if they exceed this, but with another
    error message... but again, I'm not totally sure about that).
    The message is typically for loops, but I don't know when it
    fires. Sometimes it appears for really 'small' loops when you're
    using the debugger (e.g. a for loop with 100 iterations), so maybe
    it is connected to the time a loop is running. You could place some
    trace statements in the xmlTraverse method, to see where the
    function was when the message occurs, or take the time the function
    needs and check if this might be related to the error.
    That's all I can come up with, guess there are people here
    with more insight to this...
    cheers,
    blemmo

  • Error in retrieving and parsing XML File

    Hi Folks
    I am Working on People centric user interface, While i am custimizing a application in Business application Builder i am getting this error
    " Error in retrieving and parsing XML File "
    can any body look on this and give me the solution
    it will be rewarded
    Regards
    M.S.Kumar

    Hello,
    As mentionned by SAP_TECH, avoid to use the BAB.
    Go to CRMC_BLUEPRINT_C and use the different option in the menu to customize the field group, toolbar group, events, ...
    Use the PCUI cookbook to find your way.
    Regards,
    Fred

  • Problem with JDOM and euro character

    Hi,
    I have an xml file including euro characters (�) and starting with :
    <?xml version="1.0" encoding="ISO-8859-15"?>
    as it should probably be.
    However, my problem is that (JDOM and) SAXBuilder doesn't recognize the euro characters and replace them by an unknown character '?'.
    What's wrong and how can I solve it?
    Thanks in advance.

    All right, this is a a part of my code:
    ....SAXBuilder builder = new SAXBuilder( true );
    //create the document
    //this doc contains this tag <RefVersion> data�data </RefVersion>
    Document doc = builder.build( "E:\\test.xml" );
    System.out.print( "before charAt = " + "�".charAt(0) + String.valueOf('\u20ac') );
    String str = "euro�euro";
    System.out.println( "euro�euro ->" + str.replace('\u20ac','O') );
    Element root = doc.getRootElement();
    //recup du noeud RefVersion
    Element version = root.getChild( "RefVersion" );
    str = version.getText();
    System.out.println( "nom= " + version.getName() + " data= " + str );
    System.out.println( "index de � ds euro�euro " + str.replace('\u20ac','O') );
    and the output is:
    before charAt = ��
    euro�euro ->euroOeuro
    nom= RefVersion data= data?data
    index de � ds euro�euro data?data
    So, obviously, it comes from JDOM.

  • Reading email in outlook with c# and parsing the body

    Hello,
    I don't know where to start.
    The organization I work for uses different user web forms to collect user/client feedback. Instead of having those forms populate a table, they have them emailed to me as text in the body of an email for me to decide what to do with. My (local) boss wants
    me to upload the data into our CRM. Everyone expects me to do that by hand ( which is now getting out of hand :D.)
    I need to read an outlook 2010 email in a shared mailbox (to which I do not know the account password for mail server access) and parse the body data into a data table. 
    If I can get some help understanding how to access the individual email file(s) and read the data into a string array or something that achieves this result, I can take it from there.  All suggestions are welcome however going back to the admin who
    is in another country, won't make changes and doesn't speak english are not paths I can follow.
    My glass here, is half full with the opportunity to resolve this challenge. 

    Hello,
    You can develop a VBA macro for handling new emails in the shared mailbox. I'd suggest starting from the
    Getting Started with VBA in Outlook 2010 article. If you see a shared mailbox in your Outlook profile you can subscribe to the Inbox events (for example, ItemAdd) in the following way:
    Dim WithEvents myInboxMailItem As Outlook.Items
    Private Sub myInboxMailItem_ItemAdd(ByVal Item As Object)
    Call MsgBox("Item Added", vbOKOnly, "[email protected]")
    End Sub
    Private Sub Initialize_Handler()
    Dim fldInbox As Outlook.MAPIFolder
    Dim gnspNameSpace As Outlook.NameSpace
    Set gnspNameSpace = Outlook.GetNamespace("MAPI") 'Outlook Object
    Set fldInbox = gnspNameSpace.Folders("[email protected]").Folders("Inbox")
    Set myInboxMailItem = fldInbox.Items
    End Sub
    Private Sub Application_Startup()
    Call Initialize_Handler
    End Sub
    In the ItemAdd event handler you can get all the required information parse the message body. The Outlook object model provides
     three main ways for working with item bodies: Body, HTMLBody and WordEditor.

  • Using CDATA tags in XML program and parsing to J2ME

    Hi,
    Can anybody tell me how to use CDATA tag in a xml file and parse it to my J2ME code. is there any sample code available for this? I want to use this CDATA tag for sending Binary data. plzzzzzzz Help....!

    I think what you want is to parse a CDATA text from a xml file and let this text to execute.
    Very innovative idea!
    If I am right, I think you might mix some concept.
    Java code need to be changed to ByteCode before pased to the JVM. Furthermore, Java ME code must be verified before downloading to your machine.
    I figure that your attempt is impossible.

  • I/O Assistant Query and Parse problem.

    Hi,
    I am trying to use the I/O assistant with Instrument via the serial port (com1). When I use the query and parse option it only grabs the first line and parses it. For example, my command is "gc", my instrument by nature will echo that back to serial port and when the parse occurs it only receives the "gc" rather then the entire result. Using hyperterminal I get this below. What is important to me is the second line but I only read back gc for some reason with I/O assistant. Any way to parse entire result?
    tp gc
    version: gc.library 1.3.0.0 (2003.11.03)

    In your Query and Parse step you should see a termination character window. As explained in the help window when you move the mouse over the termination character window, this character terminates the read session. The default is \n or a line feed. When your instrument sends two lines, the first line ends with a line feed and the read stops before the second line is read. Set the term character to none to read the entire line. Your instrument probably requires a termination character at the end of the string you're sending so put the termination character in the command line (i.e. gc\n).

  • Read CLOB and parse out records

    I have a table that features an XMLTYPE column containing CLOB data with XML and HTML content in it. I'm not quite sure how I can parse out the [CDATA] section and create a unique list of IDs contained within the javascript:openLink() string. Any ideas? I've tried searching the forums for how to read a CLOB and parse out each line, but didn't find the results I was looking for.
    I've provided a sample of some of the content in this column:
    <html><![CDATA[
    <P>
    <font face="Arial" SIZE="3">
    <strong>
    <span style="FONT-FAMILY: Arial">Recruiting</span>
    </strong>
    </font>
    </P>
    <P>
    <A CLASS="divHyperLink" href="javascript:openLink(1010)">2008 Newsletters</A>
    </P>
    <P>
    <A CLASS="divHyperLink" href="javascript:openLink(1009)">2007 Newsletters</A>
    </P>
    <P>
    <A CLASS="divHyperLink" href="javascript:openLink(1008)">2006 Newsletters</A>
    </P>
    ]]></html>
    I basically need to output a list of IDs:
    LinkID
    1010
    1009
    1008

    Sorry...I thought I could figure things out based on your example, but I 'm not sure if 10g supports noentityescaping clause. Do you happen to know what I would need to substitute it with? To my understanding the XMLELEMENT function takes a name for "identifier," although I'm not quite sure in this case what the identifier should be...an XML tag value?
    This is SQL I tried to run but not getting any results:
    SELECT t2.*
    FROM (SELECT pagecontent
    FROM tb_rh_page
    WHERE pageid = 1) T,
    XMLTABLE('a/@href' PASSING XMLELEMENT(noentityescaping, EXTRACTVALUE(T.pagecontent,'/p_PAGECONTENT/Controls/divTopLeft/html/text()')).EXTRACT('//a')
    COLUMNS linkid integer PATH 'ora:replace(.,"[^[:digit:]]","")') t2
    Also the HTML is a little deeper in the XML than I originally posted, so I'm not sure if I'm getting to the path correctly.
    Here's what the data looks like in the column:
    <p_PAGECONTENT>
    <PageName/>
    <Title/>
    <Roles/>
    <Controls>
    <PageTool>
    <visible>false</visible>
    </PageTool>
    <divTopLeft>
    <visible>true</visible>
    <style>font-size:larger;overflow:auto;FILTER: progid:DXImageTransform.Microsoft.Gradient(StartColorStr=#3b6f9f, EndColorStr=#e6eff6);Height:560px;background-color:#003366;border-bottom-width: 1px;border-color: #c6ddf1;border-left-width: 1px;border-right-width: 1px;border-style: Solid;border-top-width: 1px;</style>
    <html><![CDATA[
    <P>
    <font face="Arial" SIZE="3">
    <strong>
    <span style="FONT-FAMILY: Arial">Recruiting</span>
    </strong>
    </font>
    </P>
    <P>
    <A CLASS="divHyperLink" href="javascript:openLink(1010)">2008 Newsletters</A>
    </P>
    <P>
    <A CLASS="divHyperLink" href="javascript:openLink(1009)">2007 Newsletters</A>
    </P>
    <P>
    <A CLASS="divHyperLink" href="javascript:openLink(2008)">2006 Newsletters</A>
    </P>
    ]]></html>
    </divTopLeft>
    <divTopRight>
    <visible>false</visible>
    <style/>
    <html><![CDATA[]]></html>
    </divTopRight>
    <divBottomLeft>
    <visible>false</visible>
    <style/>
    <html><![CDATA[]]></html>
    </divBottomLeft>
    <divBottomRight>
    <visible>false</visible>
    <style/>
    <html><![CDATA[]]></html>
    </divBottomRight>
    </Controls>
    </p_PAGECONTENT>

  • Read CSV File and parse it

    Hi.
    I have a .csv file on my filesystem.
    Could you post few code to read it from filesystem and parse every rows in it?
    Thanks a lot.

    External tables are far easier to use... e.g.
    I have a file on my server in a folder c:\mydata called test.txt which is a comma seperated file...
    1,"Fred",200
    2,"Bob",300
    3,"Jim",50As sys user:
    CREATE OR REPLACE DIRECTORY TEST_DIR AS "c:\mydata";
    GRANT READ, WRITE ON DIRECTORY TEST_DIR TO myuser;Note: creates a directory object, pointing to a directory on the server and must exist on the server (it doesn't create the physical directory).
    As myuser:
    SQL> CREATE TABLE ext_test
      2    (id      NUMBER,
      3     empname VARCHAR2(20),
      4     rate    NUMBER)
      5  ORGANIZATION EXTERNAL
      6    (TYPE ORACLE_LOADER
      7     DEFAULT DIRECTORY TEST_DIR
      8     ACCESS PARAMETERS
      9       (RECORDS DELIMITED BY NEWLINE
    10        FIELDS TERMINATED BY ","
    11        OPTIONALLY ENCLOSED BY '"'
    12        (id,
    13         empname,
    14         rate
    15        )
    16       )
    17     LOCATION ('test.txt')
    18    );
    Table created.
    SQL> select * from ext_test;
            ID EMPNAME                    RATE
             1 Fred                        200
             2 Bob                         300
             3 Jim                          50
    SQL>
    {code}
    http://www.morganslibrary.org/reference/externaltab.html                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Importing CSV file and parsing it

    First of all I am very new to writing powershell code.  Therefore, my question could be very rudimentary, but I cannot find an answer, so please help.
    I'm trying to read a CSV file and parse it.  I cannot figure out how to access nth element without hardcoding its name.
    $data = Import-Csv $file   #import CSV file
    # read column headers (manually read the first row of the data file, or import it from other source, or ...)
    $file_dump = Get-Content $file  #OK, I'm sure there is another way to get just the first line, but that's not relevant
    $name_list = $file_dump[0].split(",")
    # access element
    $temp = $data[$i].Name  # works - but that's HARDCODING the column name into the script - what if someone changes it?
    #but what I want to do is
    $temp = $data[$i].$name_list[0]
    How do I do this in PowerShell?

    So you're asking how to get the first data point from the first column, no matter what the header is?
    Why won't you know what your input file looks like?
    You can always drop the first line of the file to remove the existing headers and then use the -Header parameter of Import-Csv to give yourself known headers to reference (this will only work if you know how many columns to expect, etc etc etc).
    http://ss64.com/ps/import-csv.html
    Don't retire TechNet! -
    (Don't give up yet - 13,085+ strong and growing)

  • Passing Arrylist from action class to jsp and parsing in jsp

    I need to do the following using struts
    I have input jsp, action class and action form associated to that. In the action class I will generate the sql based on the input from jsp page/action form, create a result set. The result set is stored as java objects in a ArrayList object.
    Now I need to pass the arraylist object to another jsp to display. Can I put the ArrayList object in the request object and pass to the success page defined for the action? If this approach is not apprpriate, please let me know correct approach.
    if this method is okay, how can I access the objects from arraylist in jsp and display their property in jsp. This java object is a java bean ( getter and setter methods on it).
    ( need jsp code)
    Can I do like this:
    <% ArrayList objList = (ArrayList)request.getattribute("lookupdata"): %> in jsp
    (***I have done request.setattribute("lookupdata", arraylistobj); in action class. ***)
    Assuming the java object has two properties, can I do the following
    <% for (iint i=0. i<objList.size;I++){ %>
    <td> what should i do here to get the first property of the object </td>
    <td> what should i do here to get the first property of the object </td>
    <% }
    %>
    if this approach is not proper, how can I pass the list of objects and parse in jsp?
    I am not sure what will be the name of the object. I can find out how many are there but i am not sure I can find out the name

    Double post.
    http://forum.java.sun.com/thread.jspa?threadID=5233144&tstart=0

  • Downloading and parsing XML during preload

    Apologies if this topic has already been covered but the
    search function on the forums doesn't appear to be working for me.
    My problem is this. I have a flex app that needs to download
    a couple of xml files during initialization that are approximately
    2-3 MB each. I'd like for the progress bar to continue showing
    until the files are downloaded and parsed and only after the files
    are downloaded and parsed, would the components start to be
    rendered since some of them will require the files to determine
    their look and feel.
    Any ideas or places I should look to in order to figure this
    out?
    Thanks
    Sang

    Greg_B,
    You could try and access the XML file via a file:// URI rather than an http:// one - this would not need the server to be started in order to get the xml/dtd loaded. You know the relative path to the dtd and xml files, and you can create a File object from there, so you can parse the xml file that way if you like.
    Here's some code to help:
    String filename = getServletContext().getRealPath("myXML.xml");
    You can then pass the filename string as a parameter to the XML (I'm assuming you're using DOM but if you aren't I think you'll still get my point) parse method.
    You can do all of this in the servlet's init() method, so it'll do that before it all loads up.
    Hope it helps
    Daniel

Maybe you are looking for

  • How do i transfer funds from one account to another in the same table?

    Say for example i want to transfer funds from UserID 1111101 to UserID  1111103. Anyone know the procedure for this. I use php/mysql and dreamweaver.

  • Error during install of Essbase server on OL 6.3 ...

    All tests are passed and all looks good until the actual install begins, then the following shows up in the log, and the gui shows failed (red X) on all installations. The wizard cannot continue because of the following error: could not load wizard s

  • Graph Plot

    Hi, I have to plot 4 plots in a graph which has 3 scales(Y1,Y2 and X).In my four plots 3 needs to plot against Y1 and 1 against Y2. I have provided cursor option in the graph.Whenever cursor is moved it has to show all 4 plots value according to curs

  • Searching inside PDF Folder

    I'm trying to do searched within a Folder containing 100's of PDF Documents for a particular Text. Windows will search but not with a PDF, it will search and return information in for example a Windows.DOC file.  This used to work until >6 months ago

  • My flash website gets stuck and doesn't load properly

    I have developed a flash website that works perfectly well when i test it on my computer. when i have loaded it onto the web it seems to get stuck. www.adamdunnesculpture.com if i do a hard refresh (Ctrl & F5) it will load properly. is there any code