I have a String which contains XML nodes within it, and am seeking to use DOM parsing to process this string to extract node values and store them in local variables.
The XML which is stored in a String variable:
<carGarage>
<car>
<make>Chrysler</make>
<color>Red</color>
</car>
<car>
<make>Musano</make>
<color>Blue</color>
</car>
</carGarage>
My Java class where I want to extract the XML values and store them in the local attributes:
public class CarGarage
{ String make, color;
public void setMake(String make)
{ this.make = make; }
public void setColor(String color)
{ this.color = color; }
public void DOMparsingMethod(Node thisNode)
{ int typeOfNode = thisNode.getNodeType();
...
}
}
What is the best way to approach this? I have looked at StringBufferInputStream but that is deprecated. I am truly quite lost.
Thankyou, Lucas.
Look at using DocumentBuilder to parse from any kind input stream. Here's an example that reads to a class like yours from a file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With