LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-03-2012, 03:33 PM   #1
segmentation_fault
Member
 
Registered: Sep 2008
Location: Ioannina, Greece
Distribution: Gentoo
Posts: 332

Rep: Reputation: 54
parsing custom xml file in java


I have been assigned a project in software engineering a part of which is verifying the syntax of an xml file and then create a custom data model. A sample of this xml syntax is :
Code:
<Program> TestProgram
<Function f1>
  <UsedFunctions>
    f2
    f3
  </UsedFunctions>
</Function>
</Program>
which means that in the c source file TestProgram.c there is a function named f1 which calls (uses) f2 and f3.
I currently try to work with DocumentBuilder like this:
Code:
try {
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  System.out.println("Trying to parse file...");
  Document document = db.parse(this.file);
  }/* try */
which complains about "f1" being inside the "Function" tag. If I get this "f1" outside the tag, eg
Code:
<Function> f1
...
</Function>
it works ok. Is there a way to check the syntax and parse the file without changing xml source file using an already existing class? Or I have to write a new syntax verification/parser myself?

Thanks in advance for any help.
 
Old 01-04-2012, 01:15 AM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 207Reputation: 207Reputation: 207
Hm, I'm surprised to hear this. What is the specific error that you receive with the unmodified XML? Can you copy and paste it here verbatim?
 
Old 01-04-2012, 06:34 AM   #3
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Is 'f1' literally that string? Because shouldn't it at least be named and its value quoted if an attribute? f1="f1"? Moving it to between the tags make it an element, no?

Last edited by Proud; 01-04-2012 at 06:35 AM.
 
Old 01-04-2012, 10:04 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Your parser seems to be saying that
Code:
<Function f1>
is not valid XML, and from my understanding of XML, that is correct. So, if you need to use the XML source as-is, then you cannot use a standard XML parser, and you will therefore have to write your own parser. It seems like a better idea to fix the (non-)XML source, and in so doing, gain the advantage of being able to use standard tools, and other benefits of XML.

--- rod.
 
Old 01-04-2012, 11:16 AM   #5
segmentation_fault
Member
 
Registered: Sep 2008
Location: Ioannina, Greece
Distribution: Gentoo
Posts: 332

Original Poster
Rep: Reputation: 54
This xml syntax was given to me in the specifications requirements (it is a project at software engineering class I'm attending), so I will ask the professor/"client" if we can change the specifications to use standard xml syntax, or I have to write my own parser. I just couldn't figure out if this syntax is wrong (which apparently is) or I was using the wrong parser. Thanks for the help.

For clarification, this is the error I get:
Code:
$ java eekp_test/imptest
Current wd is : /home/hitman/develop/sw_engineer/import
Enter filename : test.xml
Building object
Trying to parse file...
[Fatal Error] test.xml:4:17: Attribute name "f1" associated with an element type "Function" must be followed by the ' = ' character.
org.xml.sax.SAXParseException: Attribute name "f1" associated with an element type "Function" must be followed by the ' = ' character.
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:249)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
        at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
        at eekp_test.importXml.verifySyntax(importXml.java:39)
        at eekp_test.importXml.<init>(importXml.java:17)
        at eekp_test.imptest.main(imptest.java:16)

Last edited by segmentation_fault; 01-04-2012 at 11:21 AM.
 
Old 01-04-2012, 12:51 PM   #6
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 207Reputation: 207Reputation: 207
Ah there you have it. Yea, I never have personally seen an unassigned attribute like that, I don't think -- I guess that's because it isn't actually valid XML.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing XML using Sax Parser in Java vbx_wx Programming 1 07-07-2011 11:17 AM
[SOLVED] java xml parsing Wim Sturkenboom Programming 8 10-02-2009 03:35 AM
Need help in parsing XML file madhi Programming 12 07-10-2009 01:36 AM
parsing xml file from a C program golden_boy615 Programming 1 03-14-2009 09:35 AM
Parsing XML file sneha hendre Linux - Newbie 2 09-15-2008 10:55 PM


All times are GMT -5. The time now is 10:14 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration