0

I'm parsing an xml file, that has nodes with text like this:

  <?xml version="1.0"  encoding="iso-8859-1"?>
<country>
  <name> France </name>
  <city> Paris </city>
  <region>
    <name> Nord-Pas De Calais </name>
    <population> 3996 </population>
    <city> Lille </city>
  </region>
  <region>
    <name> Valle du Rhone </name>
    <city> Lyon </city>
    <city> Valence </city>
  </region>
</country>

What I want to get is values like this:

country -> name.city.region*
region  -> name.(population|epsilon).city*
name    -> epsilon
city    -> epsilon
population -> epsilon

I can't figure out a method to do that

4
  • You should not use regex for parsing xml. Use a parser instead. Look at THIS link Commented May 1, 2012 at 9:28
  • @SalmanA you don't see the question , im parsing my XML file using JDOM parser , and i want get the regular expression associate to the XML file Commented May 1, 2012 at 9:34
  • @his im not parsing the XML file with a regular expression , i want to generate the regular expression Commented May 1, 2012 at 9:39
  • Do you want to create the grammar? Like a DTD or an XSD? Commented May 1, 2012 at 9:39

1 Answer 1

2

I have added a sample program. Please continue with reading same way.

public class TextXML {

    public static void main(String[] args) {
        try {

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new File("text.xml"));

            // list of country elements
            NodeList listOfCountry = doc.getElementsByTagName("country");
            for (int s = 0; s < listOfCountry.getLength(); s++) {

                Node countyNode = listOfCountry.item(s);

                if (countyNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element countyElement = (Element) countyNode;

                    NodeList nameList = countyElement.getElementsByTagName("name");
                    // we have only one name. Element Tag
                    Element nameElement = (Element) nameList.item(0);
                    System.out.println("Name : " + nameElement.getTextContent());

                    NodeList cityList = countyElement.getElementsByTagName("city");
                    // we have only one name. Element Tag
                    Element cityElement = (Element) cityList.item(0);
                    System.out.println("City : " + cityElement.getTextContent());

                    NodeList regionList = countyElement.getElementsByTagName("region");
                    // we have only one name. Element Tag
                    Element regionElement = (Element) regionList.item(0);
                    System.out.println("Region : " + regionElement.getTextContent());

                    //continue further same way.
                }

            }

        } catch (SAXParseException err) {
            err.printStackTrace();
        } catch (SAXException e) {
            Exception x = e.getException();
            ((x == null) ? e : x).printStackTrace();

        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

you will have to test it your self.
You can also try with JAXB. This is good and clean way to read data from a XML content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.