LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   parsing custom xml file in java (http://www.linuxquestions.org/questions/programming-9/parsing-custom-xml-file-in-java-921849/)

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

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

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:
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)


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.