0

I want to parse XML in Java. The XML looks like:

<Attributes><ProductAttribute ID="359"><ProductAttributeValue><Value>1150</Value></ProductAttributeValue></ProductAttribute><ProductAttribute ID="361"><ProductAttributeValue><Value>1155</Value></ProductAttributeValue></ProductAttribute></Attributes>

My try was:

 public static void parseXml(String sb) throws Exception{


    sb = "<Attributes><ProductAttribute ID="359"><ProductAttributeValue><Value>1150</Value></ProductAttributeValue></ProductAttribute><ProductAttribute ID="361"><ProductAttributeValue><Value>1155</Value></ProductAttributeValue></ProductAttribute></Attributes>";

     Document dom;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    dom = db.parse(new InputSource(new ByteArrayInputStream(sb.getBytes("utf-8"))));

    dom.toString();
}

I wanted first see, if the parsing is going. But it doesn't.

I get the error:

 Premature end of file

Have anybody an idea, how can I parse these?

The question is not duplicate. I have read the answers of another question like my question, but the difference is the XML.

Thanks

mg.mg.92
  • 1
  • 3
  • 6
    Well you're trying to parse a string that isn't XML. You need to get the *value* of the `AttributesXml` property from the JSON, then parse *that*. It's unclear where all this is coming from or what the rest of your code looks like, but the first thing you should concetrate on is obtaining valid XML. – Jon Skeet Jul 24 '17 at 11:10
  • Firstly, get a correct XML, then normalize it and then use the getElementsByTagName method to obtain the values. – srp321 Jul 24 '17 at 11:19
  • thanks for your comments. I have changed it and get the value of the AttributesXml, but now i get a error. I have updated my question – mg.mg.92 Jul 24 '17 at 11:43
  • I am getting a new error: Premature end of file – mg.mg.92 Jul 24 '17 at 13:10

1 Answers1

0

First parse the JSON string to get the XML, then parse the xml. For instance, using JSON-java:

        JSONObject obj = new JSONObject(json);
        JSONArray arr = obj.getJSONArray("value");
        for (Object elm: arr) {
            String xml = ((JSONObject)elm).getString("AttributesXml");
            DocumentBuilderFactory documentBuilderFactory
                    = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder
                    = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse(
                    new InputSource(new StringReader(xml)));
            doSomethingWith(document);
        }

But the method parse with a String argument expects an URI as the argument, not the XML source, so you must use another one.

UPDATE:

I see that you have updated your question, and the xml is in sb; this will be:

            DocumentBuilderFactory documentBuilderFactory
                    = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder
                    = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse(
                    new InputSource(new StringReader(sb)));
            doSomethingWith(document);
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24