0
votes

I have XML data in data base (not file) i need to parse it to gave possibility write test to verify data in xml

xml (content data):

 <brid:AccountData xmlns:brid="http://billing.a1telekom.at/BridgeIT/BridgeITDefinition">
 <brid:AccountNumber>250000000032</brid:AccountNumber>
 <brid:CustomerNumber>100104653</brid:CustomerNumber>
 <brid:AccountType>NORM</brid:AccountType>
 <brid:BillCycle>M2</brid:BillCycle>
 <brid:LastInvoiceDate>0001-01-01T00:00:00.000</brid:LastInvoiceDate>
 <brid:BillThroughDate>0001-01-01T00:00:00.000</brid:BillThroughDate>
 <brid:StartDate>2016-02-26T15:27:13</brid:StartDate>
 <brid:EndDate>9999-12-31T23:59:59.000</brid:EndDate>
 <brid:AccountStatus>Active</brid:AccountStatus>
 <brid:TaxCode>U2</brid:TaxCode>
 <brid:CostCentre></brid:CostCentre>
</brid:AccountData>
<brid:PaymentData xmlns:brid="http://billing.a1telekom.at/BridgeIT/BridgeITDefinition">
 <brid:PaymentMethod>Manual</brid:PaymentMethod>
</brid:PaymentData>
<brid:MediaData xmlns:brid="http://billing.a1telekom.at/BridgeIT/BridgeITDefinition">
 <brid:AccountNumber>250000000032</brid:AccountNumber>
 <brid:MediaType>PAPIER</brid:MediaType>
 <brid:StartDate>2016-02-26T15:27:13</brid:StartDate>
 <brid:EndDate>9999-12-31T23:59:59.000</brid:EndDate>
 <brid:InvoiceName>ApuiafgjkLrjgdna Fouydf</brid:InvoiceName>
 <brid:Language>DE</brid:Language>
 <brid:LocationID>118298</brid:LocationID>
 <brid:FirstName>Fouydf</brid:FirstName>
 <brid:LastName>ApuiafgjkLrjgdna</brid:LastName>
 <brid:TitleCode></brid:TitleCode>
 <brid:TitleText></brid:TitleText>
 <brid:Prefix></brid:Prefix>
 <brid:Suffix>Cxvb</brid:Suffix>
 <brid:Currency>EUR</brid:Currency>
 <brid:Format>2</brid:Format>
 <brid:Atomized>N</brid:Atomized>
</brid:MediaData>

How i try to parse it:

p

ackage simbaOnlineReplacement;

import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import ta.maxcare.help.DBSor;




public class XMLTest2 {
    public static void main(String[] args) {

        try {
            String data = new DBSor().getContentData("106897066");

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(data));
            Document doc = dBuilder.parse(is);
            doc.getDocumentElement().normalize();
            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
            NodeList nList = doc.getElementsByTagName("MediaData");
            System.out.println("----------------------------");
            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                System.out.println("\nCurrent Element :" + nNode.getNodeName());
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;

                    System.out.println("First Name : " + eElement.getElementsByTagName("FirstName").item(0).getTextContent());

                    System.out.println("LastName : " + eElement.getElementsByTagName("LastName").item(0).getTextContent());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

and e given error:

[Fatal Error] :14:2: The markup in the document following the root element must be well-formed. org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 2; The markup in the document following the root element must be well-formed. at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source) at simbaOnlineReplacement.XMLTest2.main(XMLTest2.java:23)

2
Is the above sample xml document complete? It doesn't have a root node? - Minh Kieu
this is complete and working xml data, as i said before, it is not a file, it is data in our database - Anton_Selenium

2 Answers

1
votes

In well formed XML, you must have one root element. brid:AccountData, brid:PaymentData and brid:MediaData are at the same level, and this is wrong. You should have a root element enclosing them all, as an example.

1
votes

Your XML is a well-formed external general parsed entity, but it is not a well-formed document. The easiest way to handle it is therefore to create a simple wrapper file that references it as an external entity:

<!DOCTYPE wrapper [
<!ENTITY e SYSTEM "data.xml">
]>
<wrapper>&e;</wrapper>

and then parse the wrapper file.