-   Programming (
-   -   parsing custom xml file in java (

segmentation_fault 01-03-2012 04:33 PM

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 :

<Program> TestProgram
<Function f1>

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:

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

<Function> f1

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.

jhwilliams 01-04-2012 02:15 AM

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?

Proud 01-04-2012 07:34 AM

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?

theNbomr 01-04-2012 11:04 AM

Your parser seems to be saying that

<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.

segmentation_fault 01-04-2012 12:16 PM

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:

$ 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 javax.xml.parsers.DocumentBuilder.parse(
        at eekp_test.importXml.verifySyntax(
        at eekp_test.importXml.<init>(
        at eekp_test.imptest.main(

jhwilliams 01-04-2012 01:51 PM

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.

All times are GMT -5. The time now is 05:16 AM.