SAX and XML file, how to?

Hi,
I'm going to use JAXP and SAX to read my application XML config file.
But I'm lost, I don't know how to read the elements.
How does it function, do I have to put in the startElement() method as many "if" as entitys I need to process?
And how can I guess if an element belongs (is inside) one element or another?
Example:
<program>
<printer id="xx" type="aa">
<path> /aa/cc </path>
</printer>
<source>
<path> /aa/bb </path>
</source>
</program>
for this XML, how would you code it? this way?
startElement (String uri, String localName, String qName, Attributes attributes) {
if (localname.equals("printer")) {
//get printer attributes
} else if (localname.equals("source")) {
//get source attributes
thanks!

i have one sample code through which u can read the elements from xml file.
// File SaxLister.java
import java.io.IOException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class SAXLister {
public static void main(String[] args) throws Exception
new SAXLister(args);
public SAXLister(String[] args) throws SAXException, IOException
XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
// should load properties rather than hardcoding class name
parser.setContentHandler(new PeopleHandler( ));
parser.parse(args.length == 1 ? args[0] : "parents.xml");
/** Inner class provides DocumentHandler
class PeopleHandler extends DefaultHandler
boolean parent = false;
boolean kids = false;
public void startElement(String nsURI, String localName,
String rawName, Attributes attributes) throws SAXException {
// System.out.println("docEvents" + "startElement: " + localName + ","
// + rawName);
// Consult rawName since we aren't using xmlns prefixes here.
if (rawName.equalsIgnoreCase("name"))
parent = true;
if (rawName.equalsIgnoreCase("children"))
kids = true;
public void characters(char[] ch, int start, int length) {
if (parent) {
System.out.println("Parent: " + new String(ch, start, length));
parent = false;
} else if (kids) {
System.out.println("Children: " + new String(ch, start, length));
kids = false;
/** Needed for parent constructor */
public PeopleHandler( ) throws org.xml.sax.SAXException {
super( );
// File people.xml
<?xml version="1.0"?>
<people>
<person>
     <name>Ian Darwin</name>
     <email>http://www.darwinsys.com/</email>
     <country>Canada</country>
</person>
<person>
     <name>Another Darwin</name>
     <email type="intranet">afd@node1</email>
     <country>Canada</country>
</person>
</people>
I hope this gives u a better understanding of how SAX parser works..
Kindly note u need xml.jar and xerces.jar in ur classpath to run above program.
Regards,
Nikunj

Similar Messages

  • XML - 0112 Error on parsing using SAX and xml file in InputSource object.

    I need to parse a XML string and extract some information. I have to use SAX parser. I'm converting hte string to InputSource and then trying to parese.
    i'm getting XML-0112 error, my guess is that it is not able to locate DTD file but i tried hardcoding the whole path in DOCTYPE tag.
    i tried doing setSystemId also but no luck.
    null

    Can you post a simple test case to look at?

  • How to convert a Form to a Text file and XML file?

    Hi:
    I want to convert a Form to a Text file and XML file,but I don't know how convert?
    Please help!
    Oracle Form Builder V10.1.2.0.2
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0
    Thanks in advance!

    ..or follow the steps in Mtalink note; Doc ID: Note:196924.1
    Forms XML Conversion Feature ( Converting Forms To XML)

  • HTML and XML files open in same window(KM Navigation iView)

    Hi All,
    I have created a KM navigation which is pointing the folder inside the documents repository. This folder contains HTML and XML files. It is rendering fine. But, when I click on the file links in KM Navigation iView, it is opening in new window.Here I need to open in same window. How can I acheive this?. Please help me.
    Thanks & Regards,
    Venkatesh R

    Hi ,
    check the below thread and try options mentioned in it
    https://www.sdn.sap.com/irj/sdn/thread?threadID=72594
    Koti Reddy

  • How generate a XML file & how to save data in that file?

    hi,
    How to generate an xml file & how to add data in that xml file? is there anyone who can help me about this? Firstly i need to open a xml file and after that i need to save user data in that file? how i will do it? What will be the solution of this? please give me solution........
    with regards
    Bina

    With an API like Xerces you can parse an XML document into a DOM tree. You can do modifications on that tree manually and then write it back out as XML data.
    http://xerces.apache.org/xerces2-j/

  • Forgot to backup itunes library.itl and xml files

    I backup pretty much everything. Unfortunately I forgot about the itunes library datase files, the itubes library.itl and xml files. The hard drive has been repartioned and formated, so it's gone. I do have all the music in the exact same directory structure though. Both versions are Windows 7 x64. I aslo have my iPOD.
    What I want to figure out how to do is import the database into a new install of itunes. So the music and stuff doesn't matter. It's all there in the same set of folders, with the exact same path as before. Is there any way to create the itl and xml files containing the metadata from the iPOD?
    I wouldn't even be all that bummed if I had to let my music import from the hard drive again. I just really really want to import my playlists. That would really suck if I had to type them all in manually from the iPOD. If anyone could point me int he right direction, I would really appreciate it.

    Ok googlers from the future, here is the sultion I used.
    This solution puts everything back to normal with minimal work, but it costs you however much disk space is on your ipod. I downloaded the copytrans suite off the piratebay. Then I used the ipod copy to back up my ipod. This will copy things to folders based on tags. So you have to have a second copy of your music. Once it is backed up remote the itunes library files from the top folder of the itunes library. Start itunes, and then go to import playlist. Then you select the xml file from your ipod backup. This will import the entire library into itunes. When you sync your ipod it will say iit is going to delete everything and reload it, but it doesn't. If there was stuff that was no in the library it would delete it, but there won't be because the library came from your ipod to being with. So it just copies anything to it that wasn't there, if there is anything.
    From that point, when you want to add stuff to your library/ipod, you have to put a copy of the music in the library you made. So all the music you want on your computer and your ipod will take up double space on your hard drive because you have to put a copy in the library folder. Once it is there, you just go to add folder and select your library folder. THis will make it rescan and put the new songs in the database.
    The good part of this is you don't have to do selective ipod loading. You make the selection when you copy to the library folder. So at that point you just sync your ipod making an exact copy.
    That was the best solution for me anyway, with a big library and lots of playlists. I also never use itunes. The sole purpose of having itunes is to load my ipod. My ipod I use daily though. I don't drive anywhere without it connected to my stereo. Being able to select anything in a massive music library from my head unit while driving around is seriously awesome.

  • New PC-do I need to transfer Itunes library files (itl and xml files?)

    Hi gang,
    I followed the Itunes tutorial for Windows and moved my Itunes music library to a EHD as it was filling up my laptop. I left the itl and xml library files on the laptop hard drive as instructed.
    However, this laptop is a work PC and if I change jobs I will need to download a new version of Itunes to the new PC. I know how to import the files from the EHD but my question is the following:
    1. Do I need to back up the itl and xml library files to the EHD before I give back the work PC? It seems likely since they contain info about the playlists.
    2. Once I download Itunes on the new PC would I just copy the library files onto the hard drive or can they remain on the EHD?
    It sounds like (from reading some info from Chris CA) that I need to make a copy of the Itunes folder (which includes the itl and xml files) from the laptop hard drive and put it on the EHD inside the EHD Itunes folder. Then with the new PC load a new version of Itunes and hold the shift key when launching. Then I would select the library from the EHD.
    Is that correct?
    Thanks!
    Any help would be appreciated as I cannot find anything on this in Itunes help or the forum.

    Assuming you start with a standard configuration which is:
    The iTunes folder in My music on your c: drive and all your music in the iTunes Music folder in the iTunes folder on the c: drive.
    Starting from there, with iTunes closed, you copy the iTunes folder to an external drive, this also copies the music in the iTunes Muisc folder.
    To move to a new PC, you install iTunes, then you drag your new iTunes folder to the desktop, then you replace it by copying the iTunes folder from the external drive.
    With this set up you can also run entirely on the EHD if you want to. Just start iTunes with the shift key held down, navigate to the iTunes folder on the external drive an choose iTunes Library.itl. This could be useful if you want to dake the EHD to work.
    If you are not starting from the default position I described, some adjustments would have to be made, but it would need an exact descripition of how your folders are set up at the moment.

  • I fouled up; erased library .itl and .xml files; need a little help

    I think I see what I did wrong (my issue relates to this reply post I submitted earlier today >> https://discussions.apple.com/message/16254985#16254985 but I don't think there's any need to go back and read that post to get the gist of my problem).
    So, after knocking around and reading a whole bunch of forum posts and Apple support pubs, I now know I did something dumb.
    Simply put, last night, in the course of attempting to delete from my MacBook Pro the iTunes library file residing there (the one designed to contain actual media files, i.e., music, books, podcasts, etc.), I also deleted the iTunes Library.itl and iTunes Library.xml files.  Yes, doh! is right.
    [BTW, the idea behind deleting the media library on the MBP was because my media files are -- and have been for some time -- located on an external hard drive.  So the good news is I still have all my media.  Of course, the bad news is I lost all playlists and a host of song-specific information and I'm sure a bunch of other stuff that is organized on the iTunes Library.itl file.  (Apple's helpful info sheet on the .itl and .xml files at >> http://support.apple.com/kb/HT1660)]
    So  I opened up iTunes today and there is nothing there, i.e., looks like just opened for the first time ever.  I now know that in the absence of the .itl and .xml files (which I trashed yesterday), iTunes went ahead and created new, empty versions of these files.
    HERE'S THE QUESTION: what should or can i do now to rebuild my library?  My media files still reside on my external HD and there they must stay because there isn't remotely enough room on the MBP hard drive itself.
    I'm just sort of stumped.  And skittish.  I don't want to make another dumb mistake that will make things even worse.  Any ideas?

    Hey, sorry to hear that, but good to know  that you have all your "stuff" and its just a matter of getting it all back together in the right place.  So, I have some ideas why it happened to me and maybe why this happened to you, which may be relevant later, but first I'll first explain how I got out of this jam.
    After much investigating, I finally did a search for .itl files and ended up "finding" one in the Media folder itself, specifically in the Music folder (yeah, should have done this much earlier).  Looking around, I also found the other files and folders that are supposed to be at same directory level as "iTunes Media." 
    At this point, I just moved them back to where they were supposed to be and opened iTunes and, voila, there was my stuff (albeit not quite all of it or exactly as it should be).  Then, after making sure my Media folder was properly identified in Advanced Preferences, I checked the boxes under File -> Library -> Organize Library and iTunes started doing some activity that seemed like it was reorganizing or rechecking the organization (you can see I am no expert here).
    So like I indicated, it wasn't exactly the way it was when I last used iTunes before I had the problems (e.g., various podcasts, books, etc. werent there -- so i just added them into the Library manually, i.e., File -> Add To Library, etc.).
    Anyway, I hope this helps if you haven't already fixed your problem.  If not, I might have some other ideas I came across while still searching for solution to my problem.  Best of luck.

  • Have itl and xml files on external hd am running home premium. can  no longer access files, am told this is because they were created with a newer version of itunes. also when i try to download 10.3 it wont, just says invalid drive j and for me to try aga

    i am running vista home premium on desktop and vista basic on laptop. have placed my itunes music with .itl and .xml files onto external drive connected to another desktop running vista and can no longer access music on first two mentioned comps only on the one connected to external drive. always am told that the library cant be read because it was created using a newer version of itunes. have tried to download 10.3 but it always tells me that i have and invalid hard drive j and to try again. any ideas?

    Older versions of iTunes cannot read libraries created with newer versions of iTunes. You need to have the same version of iTunes on all your PCs. Is that the case?
    As far as the problem installing iTunes goes, see this article from b noir on dealing with the problem:
    https://discussions.apple.com/thread/2450004
    If you get to the point where you need the Microsoft Installer cleanup utility, you will need more help as it has been withdrawn by Microsoft. but I am hoping the virtual drive trick will work,

  • Best way to handle .properties and .xml files

    Hi,
    I have an application which contains so many properties and XML files, these are a part of WAR file which we deployed in
    tomcat server. Whenever we make changes to these properties and XML files we redeploy the app and start the server.
    Is there any way we can make sure that these files can take changes without redeploying or starting the server?
    Please clarify.
    Thanks.

    Sure - keep them outside of the war. Pick any directory where you can put them (a directory where the server is allowed to read of of course), load them from that directory in your application. Make sure you can reach that directory through (S)FTP to easily be able to modify them. Then all you need is a way to reload them in the application, should the information be cached.

  • Can not open dreamweaver due to missing menu.xml file, how do I fix this problem?

    Can not open dreamweaver due to missing menu.xml file, how do I fix this problem?

    @alicia - We have seen such issues before
    Allow me to quote David Powers from http://forums.adobe.com/thread/1192197
    The menus.xml file is normally in your personal configuration folder. Before going to the trouble of reinstalling, try deleting your configuration folder. Details of how to find it are here: http://forums.adobe.com/thread/494812.
    When you launch Dreamweaver, it should build a new configuration folder.

  • How to Update and XML file

    Hi,
    I'm reading an XML file using SAX api, now based on some calculations, i want to add a new Element inside the same XML file.
    <?xml version="1.0" encoding="UTF-8"?>
    <types>
    <type super = "City">
    <t>Faro</t>
    <t>Porto</t>
    <t>Helsinki</t>
    </type> 
    </types>Now in the above XML snippet, i want to add an new sub element <t> Islamabad </t>, inside types.
    Following is the point in the code where i'm stuck:
    for (int typeID = 0; typeID < typeList.size(); typeID++) {
                        Element type = (Element) typeList.get(typeID);
                        String superTypeStr = type.getAttributeValue("super").toString();
                        if (superTypeStr.equalsIgnoreCase(comboValue)) {
                             Element t = new Element("t");
                             t.addContent(typeValue);
                             //NOW HOW TO ADD THIS <t>, inside a matched type element (in this case "city"), and write it into the xml file
                             break;
    typeList is a List object, which i get using : typeList = types.getChildren("type");Hope i have explained the problem clearly, writing an XML fime from scratch is easy. but in my case i want to update an existing xml file with a new element entry.
    The XML file should need to be updated like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <types>
    <type super = "City">
    <t>Faro</t>
    <t>Porto</t>
    <t>Helsinki</t>
    <t>Islamabad</t>
    </type> 
    </types>--
    Regards Suleman

    hey mate, i remember when i had a similar issue before. I ended up using parsing the document using DOM instead of SAX. The reason being that i wanted to update the XML file and if i parsed the document using DOM i had a handle on the document object so i could subsequently update it. But the drawback is that the entire xml structure is parsed into memory, as opposed to SAX which im sure you know is event driven and memory friendly. I would use SAX for the simple process of parsing the xml file to either examine, or print out the content or both.Perhaps consider DOM?

  • Try to do my first XML Schema in SQL Server 2012 Mangement Studio(SSMS2012)-How to execute the xsd and xml files in SSMS2012?

    Hi all,
    I learmed the basic stuff of XML, DTD, DOM, etc. long time ago. Now, I came back to resume my XML journey and try to learn the XML Schemas, XPath and XQuery. I have Microsoft SQL Server 2012 Management Studio (SSMS2012) in our computer network. From
    Page 221 of the old Book "Beginning XML 2nd Edition" written by David Hunter, et.al., (published by Wrox), I copied the name5.xsd and name5.xml :
    <?xml version="1.0"?>
    <schema xmlns=http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.wrox.com/name" xmlns:target="http://www.wrox.com/name"
    elementFormDefault="qualified">
    <element name="name">
    <complexType>
    <sequence>
    <element name="first" type="string"/>
    <element name="middle" type="string"/>
    <element name="last" type="string"/>
    </sequence>
    </complexType>
    </element>
    </schema>
    <?xml version="1.0"?>
    <name
    xmlns=http://www.wrox.com/name"
    xmlns:xsi="http://www.wrox.org/2001XMLSchema-instance"
    xsi:schemaLocation="http://www.wrox.com/name name5.xsd"
    title="Mr.">
    <first>John</first>
    <middle>Frizgerald</middle>
    <last>Doe</last>
    </name>
    How can I execute these two files in my SSMS2012 for doing my first XML Schema trial?
    Please kindly help, advise and respond.
    Thanks in advance,
    Scott Chang

    Hi Eric Zhang, Thanks for your nice response.
    1) I saw the CREATE XML SCHEMA COLLECTION (Transact-SQL) and tried  its first example "Create XML schema collection in the database" in my SQL Server 2012 Management Studio (SSMS2012):
    -- Create a sample database in which to load the XML schema collection.
    -- Copied this set of code stsments from Microsoft Library (ms176009)
    -- ColesMS12_20a.sql (saved in C:/Documents/SQL Server Management Studio)
    -- 19 March 2015 1145 AM
    CREATE DATABASE SampleDB
    GO
    USE SampleDB
    GO
    CREATE XML SCHEMA COLLECTION ManuInstructionsSchemaCollection AS
    N'<?xml version="1.0" encoding="UTF-16"?>
    <xsd:schema targetNamespace="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"
    xmlns ="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
    <xsd:complexType name="StepType" mixed="true" >
    <xsd:choice minOccurs="0" maxOccurs="unbounded" >
    <xsd:element name="tool" type="xsd:string" />
    <xsd:element name="material" type="xsd:string" />
    <xsd:element name="blueprint" type="xsd:string" />
    <xsd:element name="specs" type="xsd:string" />
    <xsd:element name="diag" type="xsd:string" />
    </xsd:choice>
    </xsd:complexType>
    <xsd:element name="root">
    <xsd:complexType mixed="true">
    <xsd:sequence>
    <xsd:element name="Location" minOccurs="1" maxOccurs="unbounded">
    <xsd:complexType mixed="true">
    <xsd:sequence>
    <xsd:element name="step" type="StepType" minOccurs="1" maxOccurs="unbounded" />
    </xsd:sequence>
    <xsd:attribute name="LocationID" type="xsd:integer" use="required"/>
    <xsd:attribute name="SetupHours" type="xsd:decimal" use="optional"/>
    <xsd:attribute name="MachineHours" type="xsd:decimal" use="optional"/>
    <xsd:attribute name="LaborHours" type="xsd:decimal" use="optional"/>
    <xsd:attribute name="LotSize" type="xsd:decimal" use="optional"/>
    </xsd:complexType>
    </xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    </xsd:schema>' ;
    GO
    -- Verify - list of collections in the database.
    select *
    from sys.xml_schema_collections
    -- Verify - list of namespaces in the database.
    select name
    from sys.xml_schema_namespaces
    -- Use it. Create a typed xml variable. Note collection name specified.
    DECLARE @x xml (ManuInstructionsSchemaCollection)
    GO
    --Or create a typed xml column.
    CREATE TABLE T (
    i int primary key,
    x xml (ManuInstructionsSchemaCollection))
    GO
    -- ////The following code statements are not used in order to create SampleDB
    -- Clean up
    ---DROP TABLE T
    ---GO
    ---DROP XML SCHEMA COLLECTION ManuInstructionsSchemaCollection
    ---Go
    ---USE Master
    ---GO
    ---DROP DATABASE SampleDB
    It worked and I got the following results:
    1 4 NULL sys 2009-04-13 12:59:13.390 2012-02-10 20:16:02.097
    65536 1 NULL ManuInstructionsSchemaCollection 2015-03-19 11:47:17.660 2015-03-19 11:47:17.660
    http://www.w3.org/2001/XMLSchema
    http://schemas.microsoft.com/sqlserver/2004/sqltypes
    http://www.w3.org/XML/1998/namespace
    http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions
    But, I don't undertand (i) what SCHEMA COECTION is, (ii) what <xsd:schema targetNamespace=.....</xsd:complexType> are, (iii) How I can specify my "project specific" schema in the Microsoft SCHEMA COLLECTION to check my xml file.
    2) I dived in the XQuery programmimng in SSMS2012 in the last few weeks. I used the examles of Ad-Hoc XML File Query by Seth Delconte (in
    httpS://www.simple-talk.com/content/print.aspx?article=1756) in my SSMS2012. They worked. But I don't understand the (i) Importing XML data using a function, (ii) Using the XQuery contans()
    function to find substrings, (iii)Efficiency comparisons in the article.
    Please kindly help me in enlightening me to resolve the difficulties listed in 1) and 2).
    Thanks,
    Scott Chang

  • Newbie - XML File - How to load data and populate tables

    Hi,
    I'm newbie to XML data handling.
    I have a file that is provided in XML format with data from an external system.
    I need to load data from that file and populate some field in one or more tables.
    I have two options:
    1. Load the XML file directly from OS and populate the tables directly (I'm not sure if this is possible or not)
    2. Load the entire XML file from OS into a CLOB or a BLOB(?) and the select the data form the CLOB and populate my tables.
    I need some guidance about what is possible and how to do it.
    My preferred approach if possible is to load the data directly from OS and populate the target tables without loading into the database previously.
    Thanks for the help.
    Tech Info:
    OS : Unix / Windows
    DB Version: 11.2 or 11.1
    JF

    forum has many examples
    use search
    as example see
    Re: Load xml data in Oracle table
    Re: Load an XML file into table(s)
    Re: load a file

  • How do the itl and xml files work

    Due to some problems I have been having (now solved) I have been trying to get to grips with how these 2 files work.
    I now have a library of 37000 some songs in q:\itunes\itunes music and everything is working fine.
    However when I look in the xml file some song pointers are still to my documents\my music\itunes yet itunes still finds them. How?
    Does itunes look for the location set in edit>preferences>advanced>itunes music folder location first and then check the xml file address if it cannot find them there?
    Also if the songs are in the correct location anyway why is the xml file pointer looking at my documents.
    Does anyone know if this is abnormal? Can anyone explain the logic to me please 'cos to me it ain't logical!!
    Regards,
    Andy.

    I think iTunes must have got in a muddle as a result of the way you transfered your files to the external drive.
    It is important to realise that the iTunes Music folder location as designated in your preferences tells iTunes where to add music in the future. It is quite possible to get in a situation where iTunes looks there for newly added music, but continues to look in the old location for previously added files.
    If the xml file tells you it is accessing files in iTunes iTunes Music on your c: drive, then that is it is telling the truth. However these files are not longer in THE itunes Muisc folder , that is to say the one set in your preferences. All very confusing I am afraid.
    If your file locations get corrected when you play a track, there is something you could try.
    First close iTunes and make a copy of your xml and itl files in case you don't like what happens.
    Now open iTunes and go to you music view and select your entire library with select all.
    Right click in the selection and choose Get Info. Say yes to multimle edits.
    When you see the multipe edit Get Into screen, simply exit with OK, do nothing else. This will force iTunes to reread all your tags which may take a while if your library is large. If I geussed right, it will also update the file locations. Although I have used multiple get into in this way to correct tag into I haven't tried it to correct paths so this is just a guess - it could only possibly work for files that are in the designated iTunes Music folder in the proper place, but iTunes is still pointing to an old loaction where the files no longer exist.

Maybe you are looking for

  • Storing pdf into oracle

    Hello, I have oracle 10g expree edition. I want to store pdf files in my oracle database. How can I do it using apex. I got some forums to do this but I culdn't get what blocks and web util they are talking about. Can anyone please guide me how to do

  • Bridge on a server working with 2 mac's image rotation issue

    I wonder if anyone can help: I'm asking a question for my father-in-law who is a proffessional Photographer http://www.johnholbrookphotography.co.uk He has recently switched to Mac from PC. He has 2 support staff that collate all the photos from his

  • Condition Type in PUR

    Hi Guys, I'm looking for something in the business content that allows me to have the Condition Types used in the creation of Purchasing Orders and on the Invoice Receipt as well... Is there something I could use in order to upload this information t

  • Email to User after User created through Reconcilitation

    Hi, I want to send a mail after user gets created through Recon?Any help will be appreciated

  • WAP4410N stops responding very often

    Hi We found that they stop responding to any clients so they cannot get IP addresses, but we can ping the devices and enter the administration page. The only solution is to reboot the AP. we replaced for a new one one week ago under warranty period,